2016-08-09 30 views
0

Ich habe diese json:Invalid argument für foreach geliefert() auf Iterieren json Array

[{"nombre":"Example1","idcomplejo":"1","canchafk":"1","cantidadturnos":"35"}, 
{"nombre":"Example2","idcomplejo":"3","canchafk":"6","cantidadturnos":"29"}, 
{"nombre":"Example3","idcomplejo":"4","canchafk":"7","cantidadturnos":"6"}, 
{"nombre":"Example4","idcomplejo":"5","canchafk":"8","cantidadturnos":"4"}, 
{"nombre":"Example5","idcomplejo":"5","canchafk":"9","cantidadturnos":"2"}] 

Ich will zeigen, in meinem Tisch in meine HTML-Seite, aber ich habe diesen Fehler:

Invalid argument supplied for foreach()

Dies ist der Teil des Codes, wo ich immer die Fehlermeldung:

<table cellpadding="0" cellspacing="0"> 
    <thead> 
     <tr> 
      <th>ID Complejo></th> 
      <th># Cancha></th> 
      <th>Cantidad Turnos></th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php foreach ($cantidadturnos as $turno): ?> 
     <tr> 

      <td><?= h($turno->idComplejo) ?></td> 
      <td><?= h($turno->canchaFK) ?></td> 
      <td><?= h($turno->cantidadTurnos) ?></td> 
     </tr> 
     <?php endforeach; ?> 
    </tbody> 
</table> 

die var_dump von cantidadturnos ist der json, den ich oben setzte. Danke für die Hilfe

+2

Mögliche Duplikat [Wie extrahiere ich Daten aus JSON mit PHP?] (Http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) –

+0

['json_encode'] (http://php.net/manual/en/function.json-encode.php) - Schließen als Duplikat –

Antwort

1

Der JSON ist in Form eines JavaScript-Objekts. PHP kann es nicht lesen, damit es die json mit json_decode(), wie diese zu analysieren braucht:

$cantidadturnos = json_decode('[{"nombre":"Example1","idcomplejo":"1","canchafk":"1","cantidadturnos":"35"}, 
{"nombre":"Example2","idcomplejo":"3","canchafk":"6","cantidadturnos":"29"}, 
{"nombre":"Example3","idcomplejo":"4","canchafk":"7","cantidadturnos":"6"}, 
{"nombre":"Example4","idcomplejo":"5","canchafk":"8","cantidadturnos":"4"}, 
{"nombre":"Example5","idcomplejo":"5","canchafk":"9","cantidadturnos":"2"}]'); 

Jetzt ist $cantidadturnos in Form eines Arrays der foreach kann mit wie diese Arbeit:

foreach ($cantidadturnos as $turno) { 
    // do something 
}