Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode an array of json objects

Tags:

json

php

parsing

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.

like image 943
Byron Whitlock Avatar asked Apr 07 '10 16:04

Byron Whitlock


People also ask

What json_decode () function will return?

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.

How can access JSON decoded data in PHP?

PHP - Accessing the Decoded Values$obj = json_decode($jsonobj);


1 Answers

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"
  }
}
like image 197
Amy B Avatar answered Sep 21 '22 04:09

Amy B