Possible Duplicate:
Sort an array by a child array's value in PHP
I have the following array structure:
$en_array = array();
while(...) {
$en_array[] = $row;
}
$en_array = array (
array (
"name" => "a",
"followers" => 5,
"imageurl" => "http://images.com/img.jpg"
)
array (
"name" => "b",
"followers" => 25,
"imageurl" => "http://images.com/img.jpg"
)
array (
"name" => "c",
"followers" => 15,
"imageurl" => "http://images.com/img.jpg"
)
)
In this example I would like to order the keys of en array by the values of followers, e.g. $en_array[0]["followers"] would have the value of 25.
I’m not entirely sure if this can be done, but I hope it can.
Any help will be much appreciated :)!!
Since it looks like you’re only interested in sorting by followers, we can do this easily with PHP’s
usort.Sorting is, at its core, a process of comparing the array’s elements to each other and figuring out which ones are greater than the others.
usortallows you to use a custom comparison function for this process:compare_by_followers($a, $b)returns-1if$a['followers']is greater than$b['followers'](meaning that$ashould go before$b), returns1if$a['followers']is less than$b['followers'](meaning that$ashould come after$b), and returns0if they are equal.