php - Multidimensional Array - sort index value keys -


how sort multidimensional array indexes?

i have 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 @ last position of each array. last because append value @ end of dealing array , assign index key of 2.

the array created using this:

    foreach($combinedarray $agent) {         // @debug         // echo 'agents'.'<pre>'; print_r($agents); echo '</pre>';          if(!isset($agents[$agent[0]])) {             //never met agent, add them.             $agents[$agent[0]] = $agent;         } else {             //we seen agent, 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 still missing pass whole array separate function using

 $newarray = $this->missingkeyvaluefiller($agents);   private function missingkeyvaluefiller($array) {         foreach ($array $key => $value) {             if(!isset($value[2])) {                 $array[$key][2] = 0;             }         }         return $array;   } 

this ensures end array has index 2 value inside.

i have tried using ksort(), asort(), multisort() index 2 still appears @ end.

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

pass sort_natural 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 &$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"   } } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -