JSON Info ophalen vanaf website NS
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


Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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>[code]<?php echo $ActualTrack;?><h3></td>
<td><h3><?php echo $TrainType;?><h3></td>
</tr>
<?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>[code]<?php echo $ActualTrack;?><h3></td>
<td><h3><?php echo $TrainType;?><h3></td>
</tr>
<?php } echo "</table>"; ?>
Gewijzigd op 23/06/2024 16:17:45 door Donovan -
Lijn 20:
$RouteStations = $value['routeStations'];
En dan kan je die foreach doorlopen op $RouteStations.
$RouteStations = $value['routeStations'];
En dan kan je die foreach doorlopen op $RouteStations.
At least geen foutmeldingen meer maar wel een lege array
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
$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);
Gewijzigd op 24/06/2024 13:05:22 door Donovan -
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?
Kijk eens met var_dump($RouteStation); wat het precies is?




