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.
Syntax. The json_decode() function can take a JSON encoded string and convert into a PHP variable. The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively.
PHP - Accessing the Decoded Values$obj = json_decode($jsonobj);
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"
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With