{
"currency": "USD",
"results": [
{
"itineraries": [
{
"outbound": {
"duration": "08:35",
"flights": [
{
"departs_at": "2018-07-03T18:35",
"arrives_at": "2018-07-03T21:15",
"origin": {
"airport": "BOS",
"terminal": "A"
},
"destination": {
"airport": "YHZ"
},
"marketing_airline": "WS",
"operating_airline": "WS",
"flight_number": "3713",
"aircraft": "DH4",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "M",
"seats_remaining": 5
}
},
{
"departs_at": "2018-07-03T22:20",
"arrives_at": "2018-07-04T08:10",
"origin": {
"airport": "YHZ"
},
"destination": {
"airport": "LGW",
"terminal": "N"
},
"marketing_airline": "WS",
"operating_airline": "WS",
"flight_number": "24",
"aircraft": "7M8",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "M",
"seats_remaining": 1
}
}
]
}
}
],
"fare": {
"total_price": "653.40",
"price_per_adult": {
"total_fare": "653.40",
"tax": "178.40"
},
"restrictions": {
"refundable": true,
"change_penalties": true
}
}
},
This is json data fetched from live flights query. I want to be able to determine the number of stops for each flight using the [depart_at] field. So I have been struggling to get the number of occurrences of the [depart_at] field so as to derive how many stops(layovers) for each flight. I have already converted to php using the code below: But I am not getting the count.
<?php foreach ($itinerary['outbound']['flights'] as $flight ){
echo count($flight['flights']);
}?>
You can try like this:
$json = '{
"currency": "USD",
"results": [
{
"itineraries": [
{
"outbound": {
"duration": "08:35",
"flights": [
{
"departs_at": "2018-07-03T18:35",
"arrives_at": "2018-07-03T21:15",
"origin": {
"airport": "BOS",
"terminal": "A"
},
"destination": {
"airport": "YHZ"
},
"marketing_airline": "WS",
"operating_airline": "WS",
"flight_number": "3713",
"aircraft": "DH4",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "M",
"seats_remaining": 5
}
},
{
"departs_at": "2018-07-03T22:20",
"arrives_at": "2018-07-04T08:10",
"origin": {
"airport": "YHZ"
},
"destination": {
"airport": "LGW",
"terminal": "N"
},
"marketing_airline": "WS",
"operating_airline": "WS",
"flight_number": "24",
"aircraft": "7M8",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "M",
"seats_remaining": 1
}
}
]
}
}
],
"fare": {
"total_price": "653.40",
"price_per_adult": {
"total_fare": "653.40",
"tax": "178.40"
},
"restrictions": {
"refundable": true,
"change_penalties": true
}
}
}';
echo substr_count($json, 'departs_at');
You can try that way.
$countDepartsAt = 0;
foreach ($itinerary['outbound']['flights'] as $flight ){
if(array_key_exists('departs_at', $flight)) {
$countDepartsAt++;
}
}
echo $countDepartsAt;