我有一个数组,其中包含有关许多航班的信息。我只想要五个最低的价格。
首先,我创建一个循环按价格对数组进行排序。
其次我打印前五个数组
但这需要更多时间..我怎样才能减少这个时间?
foreach ($flights_result->json_data['response']['itineraries'] as $key => $value)
{
$mid[$key] = $value['price']['totalAmount'];
}
//Sort the data with mid descending
//Add $data as the last parameter, to sort by the common key
array_multisort($mid, SORT_ASC, $flights_result->json_data['response']['itineraries']);
// print 5 arrays
foreach ($flights_result->json_data['response']['itineraries'] as $value)
{
echo 'departureTime:' . $value['inboundInfo']['departureTime'] . '</br>';
echo 'layoverInMin:' . $value['inboundInfo']['layoverInMin'] . '</br>';
// // loop echo
foreach ($value['inboundInfo']['flightNumbers'] as $flightNumbers)
{
echo 'flightNumbers :' . $flightNumbers . '</br>';
}
echo 'durationInMin:' . $value['inboundInfo']['durationInMin'] . '</br>';
echo 'localDepartureTimeStr:' . $value['inboundInfo']['localDepartureTimeStr'] . '</br>';
echo ' arrivalTime:' . $value['inboundInfo']['arrivalTime'] . '</br>';
echo ' numStops:' . $value['inboundInfo']['numStops'] . '</br>';
//// loop
foreach ($value[' inboundInfo']['flightClasses'] as $flightClasses)
{
echo 'flightClasses name :' . $flightClasses['name'] . '</br>';
echo 'flightClasses fareClass :' . $flightClasses['fareClass'] . '</br>';
}
echo 'localArrivalTimeStr:' . $value['inboundInfo']['localArrivalTimeStr'] . '</br>';
// loop echo
foreach ($value[' carrier'] as $carrier)
{
echo 'carrier name :' . $carrier['name'] . '</br>';
echo 'carrier code :' . $carrier['code'] . '</br>';
}
echo 'amount:' . $value['price']['amount'] . '</br>';
echo ' totalAmount :' . $value['price']['totalAmount'] . '</br>';
echo 'pricePerPassenger:' . $value['price']['pricePerPassenger'] . '</br>';
echo 'currencyCode: ' . $value['price']['currencyCode'] . '</br>';
echo 'totalPricePerPassenger: ' . $value['price']['totalPricePerPassenger'] . '</br>';
echo 'includesTax: ' . $value['price ']['includesTax'] . '</br>';
echo 'destinationCountryCode:' . $value[' destinationCountryCode'] . ' </br> -------- </br>';
$count++;
if ($count > 2)
{
break;
}
}
数组示例
Array
(
[0] => Array
(
[ecpcRank] => 0
[inboundInfo] => Array
(
[aircraftTypes] => Array
(
)
[departureTime] => 1381448400000
[layoverInMin] => 1359
[flightNumbers] => Array
(
[0] => DL3672
[1] => EK204
[2] => EK923
)
[durationInMin] => 2360
[airportsExpanded] => Array
(
[0] => PHL
[1] => JFK
[2] => JFK
[3] => DXB
[4] => DXB
[5] => CAI
)
[localDepartureTimeStr] => 2013/10/10 18:40 -0500
[airports] => Array
(
[0] => PHL
[1] => JFK
[2] => DXB
[3] => CAI
)
[arrivalTime] => 1381590000000
[numStops] => 2
[flightClasses] => Array
(
[0] => Array
(
[name] => Economy
[fareClass] => 1
)
[1] => Array
(
[name] => Economy
[fareClass] => 1
)
[2] => Array
(
[name] => Economy
[fareClass] => 1
)
)
[localArrivalTimeStr] => 2013/10/12 17:00 +0200
)
[location] => Array
(
[0] => Array
(
[code] => CAI
[name] => Cairo
)
[1] => Array
(
[code] => DXB
[name] => Dubai
)
[2] => Array
(
[code] => PHL
[name] => Philadelphia
)
[3] => Array
(
[code] => JFK
[name] => New York J F Kennedy
)
[4] => Array
(
[code] => MXP
[name] => Milan Malpensa
)
)
[carrier] => Array
(
[0] => Array
(
[name] => Delta Air Lines
[code] => DL
)
[1] => Array
(
[name] => US Airways
[code] => US
)
[2] => Array
(
[name] => Emirates
[code] => EK
)
[3] => Array
(
[name] => Egyptair
[code] => MS
)
)
[bookingType] => WEBSITE
[price] => Array
(
[name] =>
[nameOTA] =>
[description] =>
[amount] => 26280
[totalAmount] => 26280
[pricePerPassenger] => 26280
[currencyCode] => EGP
[totalPricePerPassenger] => 26280
[includesTax] => 1
)
[generatedDate] => 1380212804686
[providerId] => emirates.com
[id] => MS703[CAI-MXP],EK205[MXP-JFK],US3407[JFK-PHL]|DL3672[PHL-JFK],EK204[JFK-DXB],EK923[DXB-CAI]
[originCountryCode] => EG
[bookingCode] => 13600077136293253
[destinationCountryCode] => US
[outboundInfo] => Array
(
[aircraftTypes] => Array
(
)
[departureTime] => 1380958800000
[layoverInMin] => 1050
[flightNumbers] => Array
(
[0] => MS703
[1] => EK205
[2] => US3407
)
[durationInMin] => 1940
[airportsExpanded] => Array
(
[0] => CAI
[1] => MXP
[2] => MXP
[3] => JFK
[4] => JFK
[5] => PHL
)
[localDepartureTimeStr] => 2013/10/05 09:40 +0200
[airports] => Array
(
[0] => CAI
[1] => MXP
[2] => JFK
[3] => PHL
)
[arrivalTime] => 1381075200000
[numStops] => 2
[flightClasses] => Array
(
[0] => Array
(
[name] => Economy
[fareClass] => 1
)
[1] => Array
(
[name] => Economy
[fareClass] => 1
)
[2] => Array
(
[name] => Economy
[fareClass] => 1
)
)
[localArrivalTimeStr] => 2013/10/06 11:00 -0500
)
)
[1] => Array
(
[ecpcRank] => 0
[inboundInfo] => Array
(
[aircraftTypes] => Array
(
)
[departureTime] => 1381448400000
[layoverInMin] => 1359
[flightNumbers] => Array
(
[0] => DL3672
[1] => EK204
[2] => EK923
)
[durationInMin] => 2360
[airportsExpanded] => Array
(
[0] => PHL
[1] => JFK
[2] => JFK
[3] => DXB
[4] => DXB
[5] => CAI
)
[localDepartureTimeStr] => 2013/10/10 18:40 -0500
[airports] => Array
(
[0] => PHL
[1] => JFK
[2] => DXB
[3] => CAI
)
[arrivalTime] => 1381590000000
[numStops] => 2
[flightClasses] => Array
(
[0] => Array
(
[name] => Economy
[fareClass] => 1
)
[1] => Array
(
[name] => Economy
[fareClass] => 1
)
[2] => Array
(
[name] => Economy
[fareClass] => 1
)
)
[localArrivalTimeStr] => 2013/10/12 17:00 +0200
)
[location] => Array
(
[0] => Array
(
[code] => CAI
[name] => Cairo
)
[1] => Array
(
[code] => PHL
[name] => Philadelphia
)
[2] => Array
(
[code] => DXB
[name] => Dubai
)
[3] => Array
(
[code] => JFK
[name] => New York J F Kennedy
)
)
[carrier] => Array
(
[0] => Array
(
[name] => Delta Air Lines
[code] => DL
)
[1] => Array
(
[name] => Emirates
[code] => EK
)
)
[bookingType] => WEBSITE
[price] => Array
(
[name] =>
[nameOTA] =>
[description] =>
[amount] => 28183
[totalAmount] => 28183
[pricePerPassenger] => 28183
[currencyCode] => EGP
[totalPricePerPassenger] => 28183
[includesTax] => 1
)
[generatedDate] => 1380212804689
[providerId] => emirates.com
[id] => EK928[CAI-DXB],EK203[DXB-JFK],DL6122[JFK-PHL]|DL3672[PHL-JFK],EK204[JFK-DXB],EK923[DXB-CAI]
[originCountryCode] => EG
[bookingCode] => 13600077139546083
[destinationCountryCode] => US
[outboundInfo] => Array
(
[aircraftTypes] => Array
(
)
[departureTime] => 1380966900000
[layoverInMin] => 947
[flightNumbers] => Array
(
[0] => EK928
[1] => EK203
[2] => DL6122
)
[durationInMin] => 2118
[airportsExpanded] => Array
(
[0] => CAI
[1] => DXB
[2] => DXB
[3] => JFK
[4] => JFK
[5] => PHL
)
[localDepartureTimeStr] => 2013/10/05 11:55 +0200
[airports] => Array
(
[0] => CAI
[1] => DXB
[2] => JFK
[3] => PHL
)
[arrivalTime] => 1381093980000
[numStops] => 2
[flightClasses] => Array
(
[0] => Array
(
[name] => Economy
[fareClass] => 1
)
[1] => Array
(
[name] => Economy
[fareClass] => 1
)
[2] => Array
(
[name] => Economy
[fareClass] => 1
)
)
[localArrivalTimeStr] => 2013/10/06 16:13 -0500
)
)
)