This question already has an answer here:
$my_array_to_sort=[['id'=>19381881,'value'=>4],['id'=>19970711,'value'=>65],['id'=>19231029,'value'=>18]];
How can I sort this array to get an array like this ?
(sort by 'value',high to low );
$my_sorted_array=[
['id'=>19970711,'value'=>65],
['id'=>19231029,'value'=>18],
['id'=>19381881,'value'=>4],
];
</div>
I made this function to sort every multidimensional array by one of it's columns :
function sortArrayBy($array , $column_name,$sort=SORT_DESC){
foreach ($array as $key => $row) {
$column[$key] = $row[$column_name];
}
array_multisort($column, $sort, $array);
return $array;
}
Call it like this :
<?php $my_sorted_array = sortArrayBy($my_array_to_sort,'value') ; ?>