AppDividend
Latest Code Tutorials

PHP json_encode Example | Encode JSON Data In PHP Tutorial

0

PHP json_encode Example | Encode JSON Data In PHP Tutorial is today’s topic. PHP json_encode() function converts a PHP value into a JSON value. PHP has some built-in functions to handle the JSON. Objects in PHP can be converted into JSON by using the PHP function called json_encode(). The json_encode() function returns the string, if the function works. Arrays in PHP will also can be converted into JSON when using the PHP function json_encode(). You can find the example of covert PHP Array to JSON.

What is JSON

JSON stands for JavaScript Object Notation. It is data saved in the .json file and consists of a series of key/value pairs. JSON is used to transfer the data between the server and the browser. Here is a primary example of what might be in a .json string.

PHP json_encode Example

The syntax of json_encode() function is following.

json_encode(value, options)

The value parameter is required and of type mixed.

The options Bitmask comprising of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT.

The json_encode() function returns a string, if the function works.

Let’s see the following example.

<?php

// app.php

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr)."\n";

The output is following.

 

Encode JSON Data In PHP Tutorial

Convert PHP Object to JSON 

We can convert PHP Object to JSON format using json_encode() method.

<?php

// app.php

class App {
  
}
$app = new App();
$app->title = 'Harry Potter Game';
$app->price = 20;

$jsonData = json_encode($app);
echo $jsonData."\n";

So, we have defined one class App and then created an object and set the properties and convert the object to JSON and print that JSON output. The output is following.

 

Convert PHP Object to JSON 

Convert PHP String to JSON

Let’s convert PHP String to JSON object using json_encode() function.

// app.js

$str = "hello AppDividend";
echo json_encode($str)."\n";

The output will be the same as string.

You can use the json_encode() in any PHP Framework.

Convert Multidimensional PHP Array into JSON

Let’s see an example where we can encode the multidimensional array.

<?php

// app.php

$post_data = array(
  'item' => array(
    'item_type_id' => 1,
    'string_key' => 'AA',
    'string_value' => 'Hello',
    'string_extra' => 'App',
    'is_public' => 1,
   'is_public_for_contacts' => 0
  )
);

echo json_encode($post_data)."\n";

The output of the above code is following.

 

Convert Multidimensional PHP Array into JSON

Generally, you have to use json_encode() function when you need to send an AJAX request to the server. Because JSON data is useful to transfer between client and server. So converting PHP to JSON, Javascript to JSON is easy. That is why JSON object contributes to the significant role in today’s web development.

Finally, PHP json_encode Example | Encode JSON Data In PHP Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.