What is the PHP array definition of this JSON:
[
['id' => 1, 'name' => 'TV & Home Theather'],
['id' => 2, 'name' => 'Tablets & E-Readers'],
['id' => 3, 'name' => 'Computers', 'children' => [
['id' => 4, 'name' => 'Laptops', 'children' => [
['id' => 5, 'name' => 'PC Laptops'],
['id' => 6, 'name' => 'Macbooks (Air/Pro)']
]],
['id' => 7, 'name' => 'Desktops'],
['id' => 8, 'name' => 'Monitors']
]],
['id' => 9, 'name' => 'Cell Phones']
];
Something like this outputs curly braces, which is not what I want:
$foo = array(array('id' => 1, 'name' => 'TV & Home Theater'));
var_dump(json_encode($foo));
The output is:
[{
"id": 1,
"name": "TV & Home Theater"
}]
I need this to only be brackets, no curly braces. Ideas?
1 Answer 1
OK, so what you're doing is correct. The original section of your question is PHP code representing your array.
To store it, you json_encode() it - don't worry about the curly braces. Save it to your file, then when you bring it up again you json_decode($file_contents, true) with the true parameter meaning "decode as an associative array, not an object".
Your data is then in that format again... Example:
$json = json_encode($my_array);
echo $json;
$php_again = json_decode($json, true);
print_r($php_again);
object.property_namenote: if you lose the curly braces, you'll lose the "key -> value" association and you can only store the values.