简体   繁体   English

php 按子键值对多层深的多维数组进行排序

[英]php sort multidimensional array that is multiple layers deep by sub key value

I have this array, but I need to sort the array by the 'C0' key's value我有这个数组,但我需要按“C0”键的值对数组进行排序

$tmp_array_value = array (
  '9e5dae29ec5a83d503f2e4d4b5f29f91' =>
  array (
    '007Hal007' =>
    array (
      'C0' => 2,
      'C1' => 1,
      'C2' => 1,
    ),
  ),
  '9317264ea7cc25c6f4f92bbdeb01ec63' =>
  array (
    '011210' =>
    array (
      'C0' => 3,
      'C1' => 1,
      'C2' => 1,
      'C3' => 1,
    ),
  ),
  'c911f95676eb7e5979fda3770bff1a03' =>
  array (
    '022218' =>
    array (
      'C0' => 1,
      'C1' => 1,
      'C2' => 1,
      'C3' => 1,
    ),
  )

and the first 2 key levels change, but I need to sort on the C0 {count/number} value并且前 2 个关键级别发生了变化,但我需要对 C0 {count/number} 值进行排序

I expect the result to be this:我希望结果是这样的:

$tmp_array_value = array (
  'c911f95676eb7e5979fda3770bff1a03' =>
  array (
    '022218' =>
    array (
      'C0' => 1,
      'C1' => 1,
      'C2' => 1,
      'C3' => 1,
    ),
  ),  '9e5dae29ec5a83d503f2e4d4b5f29f91' =>
  array (
    '007Hal007' =>
    array (
      'C0' => 2,
      'C1' => 1,
      'C2' => 1,
    ),
  ),
  '9317264ea7cc25c6f4f92bbdeb01ec63' =>
  array (
    '011210' =>
    array (
      'C0' => 3,
      'C1' => 1,
      'C2' => 1,
      'C3' => 1,
    ),
  ),

/i am not sure how to accomplish this with usort or the other multiple functions. /我不确定如何使用 usort 或其他多个功能来完成此操作。

Please help请帮忙

Because you have second-level keys which vary for each element in the array, you need to use array_values inside the comparison function to re-index those arrays and allow access to the C0 value for each element.因为数组中的每个元素都有不同的二级键,所以需要在比较 function 中使用array_values来重新索引这些 arrays 并允许访问每个元素的C0值。 You also need to use uasort to retain your associative keys:您还需要使用uasort来保留关联键:

uasort($tmp_array_value, function ($a, $b) {
    return array_values($a)[0]['C0'] - array_values($b)[0]['C0'];
});

print_r($tmp_array_value);

Note that since the second level arrays only have one element, you could (as pointed out by @Kevin) also use请注意,由于第二级 arrays 只有一个元素,因此您也可以(如@Kevin 指出的那样)使用

return reset($a)['C0'] - reset($b)['C0'];

to get the 'CO' value from the first array element.从第一个数组元素中获取'CO'值。

Output: Output:

Array
(
    [c911f95676eb7e5979fda3770bff1a03] => Array
        (
            [022218] => Array
                (
                    [C0] => 1
                    [C1] => 1
                    [C2] => 1
                    [C3] => 1
                )    
        )    
    [9e5dae29ec5a83d503f2e4d4b5f29f91] => Array
        (
            [007Hal007] => Array
                (
                    [C0] => 2
                    [C1] => 1
                    [C2] => 1
                )    
        )    
    [9317264ea7cc25c6f4f92bbdeb01ec63] => Array
        (
            [011210] => Array
                (
                    [C0] => 3
                    [C1] => 1
                    [C2] => 1
                    [C3] => 1
                )    
        )
)

Demo on 3v4l.org 3v4l.org 上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM