php 如何解码一组 json 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2594183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:03:15  来源:igfitidea点击:

How to decode an array of json objects

phpjsonparsing

提问by Byron Whitlock

I have an array of json objects like so:

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

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

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

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

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

json_decodewill not handle the array part and returns NULLfor this string.

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

回答by Amy B

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

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"
  }
}

回答by thetaiko

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

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

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