Vanaf de API van NS wil ik wat info ophalen om weer te geven in een tabel. Over het algemeen gaat het wel al, maar zodra ik verder in de array's wil gaan zoeken begrijp ik het niet meer. JSON Info die NS reisinfo ophaalt is (van 1 trein) bijvoorbeeld :Nu ben ik dus op zoek naar alle 'routeStations[1]' in de 2e array. Maar ik snap niet hoe. Uh.... Hulp aub? :D



                    <?php echo "<table>" ?>
                    <tr>
                        <td style="width: 75px;"><b>Vertrek</b></td>
                        <td style="width: 400px;"><b>Naar/Opmerkingen</b></td>
                        <td style="width: 50px; text-align:center"><b>Spoor</b></td>
                        <td style="width: 100px;"><b>Trein</b></td>
                    </tr>

                    <?php                      
                    foreach ($data_departures as $key => $value) {
                        $date = new DateTimeImmutable($value['plannedDateTime']);
                        $PlannedDateTime    = $value['plannedDateTime'];
                        $ActualDateTime     = $value['actualDateTime'];
                        $Direction          = $value['direction'];
                        $PlannedTrack       = $value['plannedTrack'];
                        $ActualTrack        = $value['actualTrack'];
                        $TrainType          = $value['product']['longCategoryName'];
                        $Canceled           = $value['cancelled'];
                        $Messages           = $value['messages'];
                        $RouteStations = array();
                       
                        foreach ("???" as $key => $value)
                        {
                            array_push($RouteStations, $value);
                        }
                        
                        empty($RouteStations);
                    ?>
                        <tr>
                            <td><h3><?php echo $date->format('H:i');?></h3></td>
                            <td><h3><?php echo $Direction . '</h3>' . "Via " . implode(", ",$RouteStations); ?</td>
                            <td style="text-align:center"><h3><?php echo $ActualTrack;?><h3></td>
                            <td><h3><?php echo $TrainType;?><h3></td>
                        </tr>

                    <?php } echo "</table>"; ?> 
Lijn 20:

$RouteStations = $value['routeStations'];

En dan kan je die foreach doorlopen op $RouteStations.
At least geen foutmeldingen meer maar wel een lege array

                    foreach ($data_departures as $key => $value) {
                        $Date = new DateTimeImmutable($value['plannedDateTime']);
                        $PlannedDateTime    = $value['plannedDateTime'];
                        $ActualDateTime     = $value['actualDateTime'];
                        $Direction          = $value['direction'];
                        $PlannedTrack       = $value['plannedTrack'];
                        $ActualTrack        = $value['actualTrack'];
                        $TrainType          = $value['product']['longCategoryName'];
                        $Canceled           = $value['cancelled'];
                        $Messages           = $value['messages'];
                        $RouteStation       = $value['routeStations'];
                        $RouteStationArray = array();
                        $RouteStations = implode(", ", $RouteStationArray);
                       
                        foreach ($RouteStation as $key => $Value)
                        {
                            array_push($RouteStationArray, $Value);
                        }
                        
                        empty($RouteStationsArray);
Eigenlijk hoef je toch geen nieuwe array aan te maken? $RouteStation is toch al een array?
Kijk eens met var_dump($RouteStation); wat het precies is?

Reageren