0

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.

3
  • I would really suggest you http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php Commented Jul 23, 2015 at 12:59
  • possible duplicate of Sort PHP multi-dimensional array based on key? Commented Jul 23, 2015 at 13:01
  • @GourabNag you don't have to quote the url... just paste it, it will trim the url and link to the correct page Commented Jul 23, 2015 at 13:13

2 Answers 2

3

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"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0
<?php $array='yourarray';
array_walk($array,"sorting");
function sorting(&$item,$key){
    ksort($item);
}
print_r($array);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.