1

I have an Array, Sample:

$array {
 [0] {
   [something]=1;
   [something2]=2;
     } 
 [1] {
   [something]=2;
   [something2]=4;
     }
 [2] {
   [something]=5;
   [something2]=2;
     }
}

I want to order the array based on the key something;

So it will look like:

$array {
 [0] {
   [something]=5;
   [something2]=2;
     } 
 [1] {
   [something]=2;
   [something2]=4;
     }
 [2] {
   [something]=1;
   [something2]=2;
     }
}
Max2Min
  • 13
  • 1
  • 3
  • What sorting is this? Isn't the array just [reversed](http://www.php.net/manual/en/function.array-reverse.php)? – Linus Kleen Jan 17 '11 at 18:33
  • well in the example it has been reversed but the array is ordered based on the value of the key something, if you have a look... – Max2Min Jan 17 '11 at 18:36

2 Answers2

5
function compare($x, $y) {
    return $x['something'] - $y['something'];
}

usort($input_array, 'compare');

you need to use a usort() similar to the above.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
0

Would the following suffice?

foreach($array as $key => $value){
    ksort( $array[$key] );
}
Matt Lowden
  • 2,586
  • 17
  • 19