简体   繁体   English

多维数组-排序索引值键

[英]Multidimensional Array - sort index value keys

How would you sort a multidimensional array indexes? 您将如何对多维数组索引进行排序?

I have this array structure 我有这个数组结构

Array
(
    [Amie] => Array
        (
            [0] => Amie
            [1] => 10
            [3] => 10.9%
            [4] => 0.0
            [5] => 14.3
            [6] => 2.4
            [7] => 1510.4
            [8] => 209.7
            [9] => 0
            [10] => 0.0
            [11] => 0
            [12] => 0.0
            [13] => 0.0
            [14] => 0.0%
            [15] => 6
            [17] => 100.0%
            [18] => 0.0%
            [2] => 1
        )

    [Darren D] => Array
        (
            [0] => Darren D
            [1] => 20
            [3] => 3.6%
            [4] => 0.5
            [5] => 0.0
            [6] => 0.0
            [7] => 2148.6
            [8] => 193.6
            [9] => 0
            [10] => 0.0
            [11] => 27418.4
            [12] => 6854.6
            [13] => 2.0
            [14] => 2.8%
            [16] => 2
            [17] => 0.0%
            [18] => 100.0%
            [2] => 0
        )
}

Index 2 is at the last position of each array. 索引2在每个数组的最后一个位置。 It is last because I append the value at the end of dealing with the array and assign the index key of 2. 这是最后一个,因为我在处理数组的末尾附加了值并分配了索引键2。

The array is created using this: 使用以下命令创建数组:

    foreach($combinedArray as $agent) {
        // @Debug
        // echo 'AGENTS'.'<pre>'; print_r($agents); echo '</pre>'; 
        if(!isset($agents[$agent[0]])) {
            //Never met the agent, add them.
            $agents[$agent[0]] = $agent;
        } else {
            //We already seen the agent, do maths.
            $agents[$agent[0]][1] += $agent[1];
            if(isset($agent[2]) && isset($agents[$agent[0]][2])) {
                $agents[$agent[0]][2] += $agent[2];
            } else if(isset($agent[2])) {
                $agents[$agent[0]][2] = $agent[2];
            }  
            $agents[$agent[0]][7] += $agent[7];
            $agents[$agent[0]][9] += $agent[9];
            $agents[$agent[0]][11]+= $agent[11];
        }

    }  

Then if index 2 is still missing I pass the whole array into a separate function using 然后,如果仍然缺少索引2,则将整个数组传递给使用

 $newArray = $this->missingKeyValueFiller($agents);

 private function missingKeyValueFiller($array) {
        foreach ($array as $key => $value) {
            if(!isset($value[2])) {
                $array[$key][2] = 0;
            }
        }
        return $array; 
 }

This ensures that the end array always has index 2 with some value inside. 这样可以确保end数组始终具有内部带有某些值的索引2。

I have tried using ksort() , asort() , multisort() but the index 2 still appears at the end. 我试过使用ksort()asort()multisort()但索引2仍显示在末尾。

Sorting functions accept sort_flags: http://php.net/manual/en/function.sort.php 排序功能接受sort_flags: http ://php.net/manual/en/function.sort.php

Pass SORT_NATURAL as sorting mode: 通过SORT_NATURAL作为排序模式:

ksort($array, SORT_NATURAL);

Following code: 以下代码:

   <?php

$array = [
    [
      1 => 'a',
      3 => 'b',
      20 => 'c',
      2 => 'd',
    ],
    [
      1 => 'a',
      3 => 'b',
      20 => 'c',
      2 => 'd',
    ],

];

foreach ($array as &$subarray) {
  ksort($subarray, SORT_NATURAL);
}

var_dump($array);

will give you: 会给你:

array(2) {
  [0]=>
  array(4) {
    [1]=>
    string(1) "a"
    [2]=>
    string(1) "d"
    [3]=>
    string(1) "b"
    [20]=>
    string(1) "c"
  }
  [1]=>
  &array(4) {
    [1]=>
    string(1) "a"
    [2]=>
    string(1) "d"
    [3]=>
    string(1) "b"
    [20]=>
    string(1) "c"
  }
}
<?php $array='yourarray';
array_walk($array,"sorting");
function sorting(&$item,$key){
    ksort($item);
}
print_r($array);
?>

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

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