【问题标题】:Multidimensional JSON array to multi~ php array not working多维JSON数组到多〜php数组不起作用
【发布时间】:2016-08-14 16:04:42
【问题描述】:

我是 JavaScript 和 PHP 的新手。我已经阅读了多个堆栈以获得答案,但我的 JSON 字符串有点不同。如果你问我,其实很简单。

字符串如下:

[[{"height":"444","width":"444","picture":"/image/data/122.jpg","x":0,"y":0,"currentheight":"444"},{"height":"444","width":"444","picture":"/image/data/122.jpg","y":"444","x":0,"currentheight":888},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":888,"x":0,"currentheight":1111}],[{"height":"223","width":"444","picture":"/image/data/122.jpg","y":0,"x":444,"currentheight":"223"},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":"223","x":444,"currentheight":446}]

现在我正在尝试使用json_decode($jsonstring, true) 对其进行解码,但是当我通过它的索引调用它时它只是没有得到一个值。一旦我尝试使用echo $jsonstring[0] 获取数据,我就会得到[ 作为结果。 $jsonstring[0]['width'] 甚至不返回任何内容。

是我说错了还是别的什么?

【问题讨论】:

  • 字符串末尾缺少关闭]
  • 你有一个矩阵/表格;数组数组。首先你需要解码 json $table = json_decode($jsonstring, true) 然后你可以得到一个对象 $row = $table[0]; $item = $row[0]; 现在你可以获取像 width echo 'width: ' . $item['width'];var_dump($item); 这样的属性。您需要了解您正在处理的数据结构。

标签: javascript php json multidimensional-array


【解决方案1】:

将']'添加到字符串后:

$ cat a.php
<?php
$a='[[{"height":"444","width":"444","picture":"/image/data/122.jpg","x":0,"y":0,"currentheight":"444"},{"height":"444","width":"444","picture":"/image/data/122.jpg","y":"444","x":0,"currentheight":888},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":888,"x":0,"currentheight":1111}],[{"height":"223","width":"444","picture":"/image/data/122.jpg","y":0,"x":444,"currentheight":"223"},{"height":"223","width":"444","picture":"/image/data/122.jpg","y":"223","x":444,"currentheight":446}]]';
print_r(json_decode($a, true));
?>
$ php a.php
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [height] => 444
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [x] => 0
                    [y] => 0
                    [currentheight] => 444
                )

            [1] => Array
                (
                    [height] => 444
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 444
                    [x] => 0
                    [currentheight] => 888
                )

            [2] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 888
                    [x] => 0
                    [currentheight] => 1111
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 0
                    [x] => 444
                    [currentheight] => 223
                )

            [1] => Array
                (
                    [height] => 223
                    [width] => 444
                    [picture] => /image/data/122.jpg
                    [y] => 223
                    [x] => 444
                    [currentheight] => 446
                )

        )

)

【讨论】:

    【解决方案2】:

    你可以使用

    从字符串中解析 json
    Var x=JSON.parse(Expression) .
    

    现在您可以使用 x 变量访问 JSON 对象了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-27
      • 2020-11-05
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多