tags:

views:

64

answers:

2

I have an array of json objects like so:

[{"a":"b"},{"c":"d"},{"e":"f"}]

What is the best way to turn this into a php array?

json_decode will not handle the array part and returns NULL for this string.

+4  A: 

json_decode() does so work. The second param turns the result in to an array:

var_dump(json_decode('[{"a":"b"},{"c":"d"},{"e":"f"}]', true));

// gives

array(3) {
  [0]=>
  array(1) {
    ["a"]=>
    string(1) "b"
  }
  [1]=>
  array(1) {
    ["c"]=>
    string(1) "d"
  }
  [2]=>
  array(1) {
    ["e"]=>
    string(1) "f"
  }
}
Coronatus
When the second parameter is `true`, "returned objects will be converted into associative arrays"
thetaiko
My version (PHP 5.2.9, json 1.2.1) also properly parses the json string into an array.
webbiedave
Yeah my bad. The code to scrape the json from the page was flawed. thanks.
Byron Whitlock
+4  A: 
$array = '[{"a":"b"},{"c":"d"},{"e":"f"}]';
print_r(json_decode($array, true));

Read the manual - parameters for the json_decode method are clearly defined: http://www.php.net/manual/en/function.json-decode.php

thetaiko