2016-08-14 1 views
0

Ich bin neu in JavaScript und PHP. Ich habe mehrere Stapel für Antworten gelesen, aber meine JSON-Zeichenfolge ist ein wenig anders. Es ist eigentlich ziemlich einfach, wenn du mich fragst.Mehrdimensionales JSON-Array für Multi-PHP-Array funktioniert nicht

Der String ist wie folgt:

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

Jetzt versuche ich, sie zu entschlüsseln mit json_decode($jsonstring, true), aber es funktioniert einfach nicht Wert, wenn ich es durch seinen Index nennen. Sobald ich versuche, Daten mit echo $jsonstring[0] zu bekommen, bekomme ich [ als Ergebnis. $jsonstring[0]['width'] gibt nicht einmal etwas zurück.

Rufe ich sie falsch an oder ist es etwas anderes?

+0

fehlt Schließung ']' am Ende der Schnur? – guest271314

+0

Sie haben eine Matrix/Tabelle; ein Array von Arrays. Zuerst müssen Sie die json '$ table = json_decode ($ jsonstring, true) dekodieren, dann können Sie ein Objekt' $ row = $ table [0]; '' $ item = $ row [0]; 'jetzt können Sie Hol Eigenschaften wie 'width'' echo 'width:'. $ item ['width']; 'oder' var_dump ($ item); '. Sie müssen die Datenstruktur, mit der Sie umgehen, verstehen. – Thomas

Antwort

0

Nach der Zugabe von ']' in den 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 
       ) 

     ) 

) 
0

U kann json aus einem String parsen mit

Var x=JSON.parse(Expression) . 

Jetzt können Sie JSON-Objekte zugreifen x Variable.

+0

Vergessen Sie nicht, die richtige Antwort zu markieren –