Pages

Friday, June 22, 2012

JSON format is not safe for associative arrays

 The PHP Function json_decode() will return associative arrays as objects:
This changes things when you decode your json encoded data object.

Look at the following example I found reading a Nice Post at Stackoverflow

Here's an associative Array

$config = array(
    'Frodo'   => 'hobbit',
    'Gimli'   => 'dwarf',
    'Gandalf' => 'wizard',
    );
Lets act On It

print_r($config);
print_r(json_decode(json_encode($config)));
 
 
The Output is:

Array
(
    [Frodo] => hobbit
    [Gimli] => dwarf
    [Gandalf] => wizard)
stdClass Object
(
    [Frodo] => hobbit
    [Gimli] => dwarf
    [Gandalf] => wizard)

No comments:

Post a Comment