我需要根据价格对这样的数组进行排序:
Array
(
[SJ] => Array
(
[desc] => Junior Suite
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 607.08
[status] => OK
[policy] => 1
[currency] => EU
)
[1] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 700.80
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
[MZ] => Array
(
[desc] => Doble Deluxe con hidromasaje
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 518.40
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
)
有人能给我正确的方法吗? :)
预期结果
Array
(
[MZ] => Array
(
[desc] => Doble Deluxe con hidromasaje
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 518.40
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
[SJ] => Array
(
[desc] => Junior Suite
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 607.08
[status] => OK
[policy] => 1
[currency] => EU
)
[1] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 700.80
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
)
我需要的是在价格函数中订购该数组。 所以如果我有很多解决方案,我需要拿一个价格较低的那个
答案 0 :(得分:2)
您可以像这样使用PHP函数usort
:
function sortInnerPrice($a, $b) {
if ($a['price'] == $b['price']) {
return 0;
}
return ($a['price'] < $b['price']) ? -1 : 1;
}
// First sort the inner prices
foreach ($test as $key => $val) {
usort($test[$key]['solutions'], 'sortInnerPrice');
}
function cmp($a, $b)
{
$aPrice = $a['solutions'][0]['price'];
$bPrice = $b['solutions'][0]['price'];
if ($aPrice == $bPrice) {
return 0;
}
return ($aPrice < $bPrice) ? -1 : 1;
}
// Then sort by lowest solution price
usort($yourArray, "cmp");
usort
是一个PHP函数,允许您根据需要对数组进行排序。你创建一个函数,如果它们是相同的将返回0,如果你想要$ a之前的$ a,则返回-1,如果你想要$ b之后的$ a,则返回1。
答案 1 :(得分:0)
尝试制作另一个包含所有价格的数组,然后使用array_multisort