How to sort an array on a certain variable in php? -
i have piece of var_dump of 1 of array items displayed here:
protected 'created_at' => object(carbon\carbon)[178] public 'date' => string '2014-01-23 00:00:00' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'europe/amsterdam' (length=16)
my question how can sort array on created_at
variable? should descending.
thanks effort!
uasort() should trick!
see working example here.
uasort($data, function($a, $b) { $first = $a['created_at']->format('u'); $second= $b['created_at']->format('u'); if ($first == $second) { return 0; } return ($first < $second) ? -1 : 1; });
the callback using date's timestamp (->format('u');
) compare 2 dates. please note, uasort() maintains key-value association while usort() not.
Comments
Post a Comment