Convert Array to JSON and JSON to Array in PHP | Example

On 3/04/2016

Hi! In today's post we'll see how to convert array to json and json to array in php. Knowledge of handling different kinds of data format is quite beneficial for web developers. JSON format is one such popular format and converting data from and to json format is a task you should master. PHP is quite versatile and it provides various functions to deal with json, xml, csv and other such formats. By using PHP's built-in functions you can easily create array to json converter and json to array converter scripts.

Converting Array to JSON in PHP

In order to convert php array to json, we have to use the PHP function json_encode() which takes up an array as a parameter and converts into a JSON string which you can process further or store in a file. Below is the php script that takes up an array and convert it into json.

Array to JSON Converter Script

<?php
// define array
$arr = Array (
    "0" => Array (
        "name" => "Michael Bruce",
        "designation" => "System Architect"
    ),
    "1" => Array (
        "name" => "Jennifer Winters",
        "designation" => "Senior Programmer"
    ),
    "2" => Array (
        "name" => "Donna Fox",
        "designation" => "Office Manager"
    )
);

// convert array to json
$json = json_encode($arr);
echo $json;

// produces output

// [{"name":"Michael Bruce","designation":"System Architect"},{"name":"Jennifer Winters","designation":"Senior Programmer"},{"name":"Donna Fox","designation":"Office Manager"}]
?>

Above we have an array of arrays which contain three sets of data and encoded it into json string.

Add Name to JSON String

Want the generated json string to have a name? Then simply encode json like this.

<?php
$json = json_encode(array('data' => $arr));

// produces output

// {"data":[{"name":"Michael Bruce","designation":"System Architect"},{"name":"Jennifer Winters","designation":"Senior Programmer"},{"name":"Donna Fox","designation":"Office Manager"}]}
?>

Converting JSON to Array in PHP

This is the exact reverse process of what we have done above i.e., convert json to php array. For this you have to use the json_decode() function which takes up a valid json string and converts it into an array. Here is the example for doing it.

JSON to Array Converter Script

<?php
// define json
$json = '[
    {
        "name": "Michael Bruce",
        "designation": "System Architect"
    },
    {
        "name": "Jennifer Winters",
        "designation": "Senior Programmer"
    },
    {
        "name": "Donna Fox",
        "designation": "Office Manager"
    }
]';

// convert json to array
$arr = json_decode($json, true);
print_r($arr);

// produces output

// Array ( [0] => Array ( [name] => Michael Bruce [designation] => System Architect ) [1] => Array ( [name] => Jennifer Winters [designation] => Senior Programmer ) [2] => Array ( [name] => Donna Fox [designation] => Office Manager ) )
?>

As simple as that, you got the original array back.

Also check for inserting json into mysql and exporting mysql to json file.

That was all about converting array to json and json to array in php.

No comments:

Post a Comment

Contact Form

Name

Email *

Message *