Php 如何使用sube值对多维数组进行排序
我有一个包含酒店阵列的阵列Php 如何使用sube值对多维数组进行排序,php,arrays,Php,Arrays,我有一个包含酒店阵列的阵列 每个(酒店)数组都有(房间)数组,其中包含房间数据,如此房间的价格和房间号 我想得到五家客房价格最低的酒店,这五家酒店被称为(price\u str) 重要注意事项: 意外情况下的阵列数 意外房间的阵列数 我在这个项目中使用PHP5.3。所以一些新的php功能不起作用 // the first hotel info array id:52548 Le Meridien Heliopolis 51 Al Orouba Street Helio
- 每个(酒店)数组都有(房间)数组,其中包含房间数据,如此房间的价格和房间号
- 我想得到五家客房价格最低的酒店,这五家酒店被称为(price\u str)
// the first hotel info array
id:52548
Le Meridien Heliopolis
51 Al Orouba Street Heliopolis Po Box 2928
stars 5
rooms 255
//description
Set in one of the finest residential districts of Cairo, the Le Meridien Heliopolis
is ideally located near the Cairo International Conference Centre,
// image
http://cdn.wego.com/gazo/88/2f1ceaef3b36d83d13bb01b0f413298bb2e42514/137974_B.jpg
// this array contains rooms info in this hotel
Array
(
[0] => stdClass Object
(
[id] => 119-9
[price_str] => 95
[currency_code] => USD
[currency_sym] => US$
[provider_name] => Le Meridien
[provider_code] => lemeridien.com
[ex_tax] => 1
[is_direct] => 1
)
[1] => stdClass Object
(
[id] => 6-1
[price_str] => 104
[currency_code] => USD
[currency_sym] => US$
[provider_name] => Booking.com
[provider_code] => booking.com
[ex_tax] =>
[is_direct] =>
)
[2] => stdClass Object
(
[id] => 88-201
[price_str] => 104
[currency_code] => USD
[currency_sym] => US$
[provider_name] => Wego
[provider_code] => wego.com
[ex_tax] =>
[is_direct] =>
)
)
// the second hotel info
id:52629
Holidays Express Hotel
2 Gameat El Dowal El Arabyia St.,Sphinx Square , Mohandessin
stars 4
rooms
// desciption
With its central location, Holidays Express Hotel is within easy reach of most
tourist attractions and business addresses in Cairo.
// image
http://cdn.wego.com/gazo/43/78be2c05a7069febbfc15b88d40af58cc8871751/7350810_B.jpg
// the rooms info for this hotel
Array
(
[0] => stdClass Object
(
[id] => 6-1
// i want to sort by lowest price_str
[price_str] => 63
[currency_code] => USD
[currency_sym] => US$
[provider_name] => Booking.com
[provider_code] => booking.com
[ex_tax] =>
[is_direct] =>
)
[1] => stdClass Object
(
[id] => 88-201
[price_str] => 63
[currency_code] => USD
[currency_sym] => US$
[provider_name] => Wego
[provider_code] => wego.com
[ex_tax] =>
[is_direct] =>
)
[2] => stdClass Object
(
[id] => 70-1
[price_str] => 69
[currency_code] => USD
[currency_sym] => US$
[provider_name] => DHR.com
[provider_code] => dhr.com
[ex_tax] => 1
[is_direct] =>
)
)
- 找到每家酒店的最低价格,将价格存储在数组中,按酒店索引索引
- 按价格对数组排序
- 获取前5个结果
您是否从数据库中获取酒店详细信息?我想您可以使用usort实现此目的。请参阅本文:包含所有这些数据的主变量称为$hotels->json_data->hotels..如何在代码中使用它?显示我问题中发布的数组…问题是我无法在首先循环hotels之前知道price_str..那么为什么要这样做$price_list=array()$price\u list
存储每家酒店的最低价格房间。我没有任何意义。。因为你以为我知道价格……事实上,我不可能先知道所有酒店的价格。!!让我们
<?php
$price_list = array(); // the lowest price of each hotel stroes in this array, indexed by the index of each hotel in $hotels array
foreach ($hotels->json_data->hotels as $hotel_index => $hotel)
{
// find out the lowest price of this hotel
$min_price = 0;
foreach ($hotel->room_rates as $room)
{
if ($min_price == 0 || $room->price_str < $min_price)
{
$min_price = $room['price_str'];
}
}
$price_list[$hotel_index] = $min_price;
}
// sort by price
asort($price_list, SORT_NUMERIC);
// fetch the top hotel
$count = 0;
$list = array();
foreach ($price_list as $index => $price)
{
$list[] = $hotels->json_data->hotels[$index];
$count++;
if ($count > 5)
{
break;
}
}