如何解码json对象数组

Byr*_*ock 5 php parsing json

我有一个像这样的json对象数组:

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

将此转换为php数组的最佳方法是什么?

json_decode将不处理数组部分并返回NULL此字符串.

Amy*_*y B 18

json_decode()这样做.第二个参数将结果转换为数组:

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"
  }
}
Run Code Online (Sandbox Code Playgroud)


the*_*iko 6

$array = '[{"a":"b"},{"c":"d"},{"e":"f"}]';
print_r(json_decode($array, true));
Run Code Online (Sandbox Code Playgroud)

阅读手册 - json_decode方法的参数已明确定义:http: //www.php.net/manual/en/function.json-decode.php