-1

Possible Duplicate:
How to sort a multidimensional array by a certain key?

I would like to sort an array, which looks like below, by the key value:

 $arr = ([0]=>Int(5)
         [1]=> Array ( [0]=>Int(4) , [1]=>String(10) , [2]=>String(22) , ['prop']=>Int(2))
         [2]=> Array ( [0]=>Int(4) , [1]=>String(10) , [2]=>String(22) , ['prop']=>Int(2))
         [3]=> Array ( [0]=>Int(4) , [1]=>String(10) , [2]=>String(22) , ['prop']=>Int(2))
        )

So, as a result i would like to have the $arr sorted by the ['prop']'s value.

I've tried bubble sort but without any result.

Community
  • 1
  • 1
Emil Dumbazu
  • 662
  • 4
  • 22
  • 37

1 Answers1

0
usort($array, function($a, $b) {
    if($a['prop'] == $b['prop']) return 0;
    return ($a['prop'] < $b['prop']) ? -1 : 1;
});

Note: this only works if you have PHP >5.3 (because it uses an anonymous function)

Jeroen
  • 13,056
  • 4
  • 42
  • 63