dpcj40970 2016-08-14 16:04
浏览 61
已采纳

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

I'm new to JavaScript and PHP. I have read multiple stacks for answers but my JSON string is a little different. It's actually pretty easy if you ask me.

The string is as follows:

[[{"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}]

Now I'm trying to decode it with json_decode($jsonstring, true), but it just doesn't get a value when I call it by it's index. As soon as I try to get data by using echo $jsonstring[0] I get [ as the result. $jsonstring[0]['width'] doesn't even return anything.

Am I calling them wrong or is it something else?

  • 写回答

2条回答 默认 最新

  • doumiang2297 2016-08-14 16:11
    关注

    After adding ']' to the string:

    $ 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
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?