多维JSON数组到多~php数组无法正常工作

时间:2016-08-14 16:04:43

标签: javascript php json multidimensional-array

我是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']甚至没有返回任何内容。

我称他们错了还是别的什么?

2 个答案:

答案 0 :(得分:0)

你可以使用

从字符串中解析json
<div itemprop="itemReviewed" itemscope itemtype="http://schema.org/Thing">
    <!-- … -->
</div>

<span itemprop="author" itemscope itemtype="http://schema.org/Person">
  <!-- … -->
</span>

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

答案 1 :(得分:0)

在字符串中添加']'后:

$ 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
                )

        )

)