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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line