php sort associative multidimensional array [duplicate]

时间:2022-05-23 21:38:56

This question already has an answer here:

这个问题在这里已有答案:

I have a big array with child array with child arrays on it.

我有一个带有子数组的大数组,上面有子数组。

The scheme is like this:

该计划是这样的:

$bigArray['apple'][]  = array('shop1', 'Fruit', '7');
$bigArray['apple'][]  = array('shop2', 'Fruit', '5');
$bigArray['pear'][]   = array('shop2', 'Fruit', '3.5');
$bigArray['banana'][] = array('shop1', 'Fruit', '2');

So in this case $bigArray['apple'] contains 2 arrays.

所以在这种情况下$ bigArray ['apple']包含2个数组。

First thing is, I want to sort this arrays from the 3rd parameter(price) in ascending order (lowest price to highest price) So when printed it will display:

首先,我想从第三个参数(价格)按升序(最低价格到最高价格)排序这个数组所以当打印它将显示:

'shop2', 'Fruit', '5' ->lowest price
'shop1', 'Fruit', '7'

Second thing is, I want to sort the entire array $bigArray in ascending order (lowest price to highest price again); Now since arrays like $bigArray['apple'] are already sorted, in the $bigArray sort only $bigArray['apple'][0] first array will be taken into consideration because [0] will be the lowest price of this child array, already sorted. So in the end when printed $bigArray will display:

第二件事是,我想按升序排序整个数组$ bigArray(最低价再次到最高价);现在由于$ bigArray ['apple']这样的数组已经排序了,在$ bigArray排序中只考虑$ bigArray ['apple'] [0]第一个数组,因为[0]将是这个孩子的最低价格数组,已经排序。所以最后打印$ bigArray时会显示:

'shop1', 'Fruit', '2'
'shop2', 'Fruit', '3.5'
'shop2', 'Fruit', '5'
....

I have been struggling with usort but is far more complicated for me to work with multidimensional associative arrays.

我一直在努力使用usort,但对于我来说使用多维关联数组要复杂得多。

Thank you.

谢谢。

1 个解决方案

#1


1  

I think that's the solution for what you want to achieve:

我认为这是您想要实现的目标的解决方案:

<?php

$bigArray['apple'][]  = array('shop1', 'Fruit', '7');
$bigArray['apple'][]  = array('shop2', 'Fruit', '5');
$bigArray['pear'][]   = array('shop2', 'Fruit', '3.5');
$bigArray['banana'][] = array('shop1', 'Fruit', '2');



foreach ($bigArray as &$item)  {

    uasort($item, function($a, $b) {        
        if ($a[2] == $b[2]) {
            return 0;
        }    

        return ($a[2] < $b[2]) ? -1 : 1;    

    });
}

uasort($bigArray, function($a, $b) { 
    if ($a[0][2] == $b[0][2]) {
        return 0;
    }    

    return ($a[0][2] < $b[0][2]) ? -1 : 1;    
});

foreach ($bigArray as $k => $v) {

    foreach ($v as $item)
    {
        echo $k.': '.$item[0].' '.$item[1].' '.$item[2]."<br />";            
    }

}

#1


1  

I think that's the solution for what you want to achieve:

我认为这是您想要实现的目标的解决方案:

<?php

$bigArray['apple'][]  = array('shop1', 'Fruit', '7');
$bigArray['apple'][]  = array('shop2', 'Fruit', '5');
$bigArray['pear'][]   = array('shop2', 'Fruit', '3.5');
$bigArray['banana'][] = array('shop1', 'Fruit', '2');



foreach ($bigArray as &$item)  {

    uasort($item, function($a, $b) {        
        if ($a[2] == $b[2]) {
            return 0;
        }    

        return ($a[2] < $b[2]) ? -1 : 1;    

    });
}

uasort($bigArray, function($a, $b) { 
    if ($a[0][2] == $b[0][2]) {
        return 0;
    }    

    return ($a[0][2] < $b[0][2]) ? -1 : 1;    
});

foreach ($bigArray as $k => $v) {

    foreach ($v as $item)
    {
        echo $k.': '.$item[0].' '.$item[1].' '.$item[2]."<br />";            
    }

}