Reverse the order of a multiple-elements array in PHP -
this question has answer here:
- how can sort arrays , data in php? 7 answers
i looked quite bit around (like here array-sort-comparisons), , tried figure out how sort (or reverse) multiple-elements array. in vain.
i have 3 elements in array, , want sort "year" asc:
array(52) { [0]=> array(3) { ["id"]=> string(2) "si" ["year"]=> string(4) "2012" ["value"]=> string(7) "3711339" } [1]=> array(3) { ["id"]=> string(2) "si" ["year"]=> string(4) "2011" ["value"]=> string(7) "3810626" } [2]=> array(3) { ["id"]=> string(2) "si" ["year"]=> string(4) "2010" ["value"]=> string(7) "3714946" }
how possible? somehow 1 needs able specify of 3 elements "key" basis of sorting.
thanks hints!
use usort()
custom comparison function:
usort($arr, function (array $a, array $b) { return $a['year'] - $b['year']; });
Comments
Post a Comment