PHP - 如何通过比较两个不同的子数组值对多维数组进行排序

PHP  - 如何通过比较两个不同的子数组值对多维数组进行排序

问题描述:

Im so tired of finding the way to sort this multidimensional array. I have tried usort() but it doesnt sort the way I want to. Or just maybe I could not figure out the correct logic to use. usort() seems hard to understand for me.

I wanted to sort my data(example below), by finding first which has the higher value (between keys a and b) for each of the sub arrays. And then the sub array which has the highest value either from its keys a or b will be on top.

For example this array:

Array
(
    [0] => Array
        (

         [a]=>5
         [b]=>
         [c]=>apple

        )

    [1] => Array
        (

         [a]=>5
         [b]=>7
         [c]=>guava

        )

    [2] => Array
        (

         [a]=>6
         [b]=>
         [c]=>banana

        )
    [3] => Array
        (

         [a]=>5
         [b]=>
         [c]=>avocado

        )

)

should be sorted like this:

Array
(
    [0] => Array
        (

         [a]=>5
         [b]=>7
         [c]=>guava

        )

    [1] => Array
        (

         [a]=>6
         [b]=>
         [c]=>banana

        )

    [2] => Array
        (

         [a]=>5
         [b]=>
         [c]=>apple

        )
    [3] => Array
        (

         [a]=>5
         [b]=>
         [c]=>avocado

        ) 

So how exactly I am able to do this? Im so confused how to use usort. What is the best PHP function for sorting this?

我已经厌倦了找到排序这个多维数组的方法。 我试过 usort()但它没有按我想要的方式排序。 或者只是也许我无法弄清楚要使用的正确逻辑。 usort() code>对我来说似乎很难理解。 p>

我想通过首先找到具有更高价值的数据来排序我的数据(例如下面的例子) >对于每个子阵列,在键a和b em>之间。 然后,从其键a或b中具有最高值的子数组将位于顶部。 p>

例如此数组: p>

  Array 
(
 [0] =>数组
(
 
 [a] => 5 
 [b] => 
 [c] => apple 
 
  )
 
 [1] =>数组
(
 
 [a] => 5 
 [b] => 7 
 [c] => guava 
 
)\  n 
 [2] =>数组
(
 
 [a] => 6 
 [b] => 
 [c] => banana 
 
)
 [3  ] =>数组
(
 
 [a] => 5 
 [b] => 
 [c] => avocado 
 
)
 
)
   代码>  pre> 
 
 

应按如下方式排序: p>

  Array 
(
 [0] => Array 
(\  n 
 [a] => 5 
 [b] => 7 
 [c] => guava 
 
)
 
 [1] =>数组
(
 \  n [a] => 6 
 [b] => 
 [c] =>香蕉
 
)
 
 [2] =>数组
(
 
 [a]  ] => 5 
 [b] => 
 [c] =&g  t; apple 
 
)
 [3] => 数组
(
 
 [a] => 5 
 [b] => 
 [c] => avocado 
 
)
  code>  pre> 
 \  n 

那么我究竟能做到这一点? 我很困惑如何使用usort。 用于排序的最佳PHP函数是什么? p> div>

Your comparison function:

function cmp($a,$b){
    // clean up the keys so they don't cause weird cross-type comparisons
    (empty($a['a']) ? $a['a'] = 0 : NULL;
    (empty($a['b']) ? $a['b'] = 0 : NULL;
    (empty($b['a']) ? $b['a'] = 0 : NULL;
    (empty($b['b']) ? $b['b'] = 0 : NULL;

    /// figure out the pertinent value to compare
    $value_from_a_to_compare = max($a['a'],$a['b']);
    $value_from_b_to_compare = max($b['a'],$b['b']);

    if($value_from_a_to_compare ==  $value_from_b_to_compare) {
        return 0;
    }

    if($value_from_a_to_compare >  $value_from_b_to_compare) {
        return 1;
    } else {
       return -1;
    }


}

Call this from usort

usort($your_array,'cmp');
var_dump($your_array);

There are several methods, including writing the function written by dethtron5000 that would enable you to use usort.

I will describe a different suggestion: create a secondary array, in which you put the highest numbers from the first array and then use array_multisort to sort both, using the values from the secondary array.

$helper = array();
foreach ($data as $value)
{
    $helper[] = max($value['a'],$value['b']);
}
array_multisort($helper,$data);

The way usort() works is that it requires a way to compare the elements of the array to each other to decide what comes first. You provide it a function, and it calls that function for each element pair in the input array. If the function returns a negative value, it means the first argument is less than the second argument and should come before it in the sorted array. The opposite is true if it returns a positive value.

Let's say you're comparing two sub-arrays s1 and s2. Using the logic you provided, s1 is "less than" s2 -- that is, s1 should come before s2 -- if max(s1.a,s1.b) > max(s2.a,s2.b). Thus, the sort function should return a negative value if this is true. If you subtract s1's max from s2's max, this should yield the proper result.

Try this:

function sort_function(array $arr1, array $arr2) {
    $arr1_val = max($arr1['a'],$arr1['b']);
    $arr2_val = max($arr2['a'],$arr2['b']);
    return $arr2_val - $arr1_val;
}

usort($arr,sort_function);

Assuming $arr is your array.