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;
     }
}
2
  • What sorting is this? Isn't the array just reversed? Commented Jan 17, 2011 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... Commented Jan 17, 2011 at 18:36

2 Answers 2

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

usort($input_array, 'compare');

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

Sign up to request clarification or add additional context in comments.

2 Comments

+1 cause I didn't know this. I just wrote an algorithm to sort it manually!
Thanks works great and saved alot of time, i was thinking of doing it the complicated way
0

Would the following suffice?

foreach($array as $key => $value){
    ksort( $array[$key] );
}

2 Comments

Ah, sorry - I thought that's what you wanted to do; sort by the key 'something'.
np, but thanks for trying, The solution is posted by dqhendricks

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.