dpcj40970 2016-08-14 08: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 08: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部