Question: how to sort multi dimensional array with object?
Status : I've an array as following.
array(3) {
[0]=>
object(Photo_model)#25 (5) {
["id"]=>
int(5)
["file_name"]=>
string(36) "A49361605AE049D687CDC3FEAF7D3236.jpg"
["user_id"]=>
int(1)
["challenge_id"]=>
string(1) "2"
["score"]=>
int(19)
}
[1]=>
object(Photo_model)#28 (5) {
["id"]=>
int(2)
["file_name"]=>
string(36) "A49361605AE049D687CDC3FEAF7D3236.jpg"
["user_id"]=>
int(1)
["challenge_id"]=>
string(1) "2"
["score"]=>
int(10)
}
[2]=>
object(Photo_model)#29 (5) {
["id"]=>
int(3)
["file_name"]=>
string(36) "A49361605AE049D687CDC3FEAF7D3236.jpg"
["user_id"]=>
int(1)
["challenge_id"]=>
string(1) "2"
["score"]=>
int(15)
}
}
I tried to sort above array by score. I created a function as follow.
aarsort (&$array, 'score');
function aarsort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
arsort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
But it is not working. How can I sort the multidimensional array by key (score
)?
result should be by id => 5,3,2