Remco van Happen Remco van Happen - 3 months ago 16
Javascript Question

Multidimensional JSON array to multi~ php array not working

I'm new to javascript and php. I have read multiple stacks for answers but my JSON string is a little different. Actually pretty easy if you ask me..

The string is as followed: [[{"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 as soon as 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?
Thanks in advance

Answer

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
                )

        )

)