代码之家  ›  专栏  ›  技术社区  ›  Byron Whitlock

如何解码json对象数组

  •  5
  • Byron Whitlock  · 技术社区  · 15 年前

    我有一个json对象数组,如下所示:

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

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

    json_decode 将不处理数组部分并返回 NULL 为了这根绳子。

    2 回复  |  直到 10 年前
        1
  •  18
  •   Amy B    15 年前

    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"
      }
    }
    
        2
  •  6
  •   thetaiko    15 年前
    $array = '[{"a":"b"},{"c":"d"},{"e":"f"}]';
    print_r(json_decode($array, true));
    

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

    推荐文章