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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度