Search code examples
phparrayssortingmultidimensional-array

Sort the keys inside of each row of a 2d array


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. It is last because I append the value at the end of dealing with the array and assign the index key of 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

 $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.

I have tried using ksort(), asort(), multisort() but the index 2 still appears at the end.


Solution

  • Sorting functions accept sort_flags: http://php.net/manual/en/function.sort.php

    Pass SORT_NATURAL as sorting mode:

    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"
      }
    }