php
PHP User Array Sort
PHP User Array Sort
Ever had an multidimensional array that has the values that you need to sort somewhere that requires PHP User Array Sort, aka uasort()?
Look no further, this is how you handle it, all quick and easy like.
Given an array like this:
Array
(
[1] => Array
(
[dateworked] => 2014-03-08
[hours] => 8
)
[2] => Array
(
[dateworked] => 2014-03-07
[hours] => 12
)
[3] => Array
(
[dateworked] => 2014-03-09
[hours] => 9
)
)
And requiring that they be in order by the 'dateworked' key, one must only run a quick uasort with an inline function, like so:
uasort($rows, (function($a,$b) {
return strtotime($a['dateworked']) > strtotime($b['dateworked']);
}));
And voila! They are now sorted:
Array
(
[1] => Array
(
[dateworked] => 2014-03-07
[hours] => 12
)
[2] => Array
(
[dateworked] => 2014-03-08
[hours] => 8
)
[3] => Array
(
[dateworked] => 2014-03-09
[hours] => 9
)
)
Last Updated: 2014-03-03 10:38:44