Skip to content

How to Create and Parse JSON Data in PHP?

This is a quickie simple post on how to create and parse the JSON(Java Script object notion) data format of using array of PHP. It’s a very simple ,blow listed code allows you to perform this task. we can parse it with jQuery to display it in client side (Browser – using ajax technologies).

Here is the Example :

PHP array to JSON Object

1,'name'=>'Arjun','country'=>'India','website' => 'arjunphp.com'),
   array('id'=>2,'name'=>'Pravinh','country'=>'India','website' => 'arjun.net.in')
);

echo json_encode($json_data);

// Result:
// [{"id":1,"name":"Arjun","country":"India","website":"arjunphp.com"},{"id":2,"name":"Pravinh","country":"India","website":"arjun.net.in"}]
?>

JSON Object to PHP array

 stdClass Object
        (
            [id] => 1
            [name] => Arjun
            [country] => India
            [website] => arjunphp.com
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => Pravinh
            [country] => India
            [website] => arjun.net.in
        )

)
*/

A json_encode() example showing some options in use

',"'bar'",'"baz"','&blong&', "\xc3\xa9");

echo "Normal: ",  json_encode($a), "\n";
// output: ["","'bar'","\"baz\"","&blong&","\u00e9"]

echo json_encode($a, JSON_HEX_TAG), "\n";
// output: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]

echo json_encode($a, JSON_HEX_APOS), "\n";
// output: ["","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]

echo json_encode($a, JSON_HEX_QUOT), "\n";
// output: ["","'bar'","\u0022baz\u0022","&blong&","\u00e9"]

echo json_encode($a, JSON_HEX_AMP), "\n";
// output: ["","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]

echo json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
// output: ["","'bar'","\"baz\"","&blong&","é"]

echo json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";
// output: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

$b = array();

echo "Empty array output as array: ", json_encode($b), "\n";
// output: Empty array output as array: []

echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";
// output: Empty array output as object: {}

$c = array(array(1,2,3));

echo "Non-associative array output as array: ", json_encode($c), "\n";
// output: Non-associative array output as array: [[1,2,3]]

echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";
// output: Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

$d = array('foo' => 'bar', 'baz' => 'long');

echo "Associative array always output as object: ", json_encode($d), "\n";
// output: Associative array always output as object: {"foo":"bar","baz":"long"}

echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
// output: Associative array always output as object: {"foo":"bar","baz":"long"}

?>
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments