Reputation: 4173
I have a Multilevel Array
(See array structure picture below) and I need to get the sub-nested array with the higher date value
.
I was wondering if there is a straight forward way to sort sub-nested arrays by date value
or get the highest date value
?
Array Map
Upvotes: 1
Views: 1299
Reputation: 37365
To do that, usort() function will be useful:
usort($rgData, function($rgX, $rgY)
{
$x = strtotime($rgX['date']);
$y = strtotime($rgY['date']);
return $x<$y?-1:$x!=$y;
});
//var_dump($rgData);
if you want to get highest value, then it will be ['date']
key of last element after doing the sort above.
Edit: if you're sure that format will be exactly same as on picture always, you can use direct string comparison via strcmp
(that would be probably faster)
Upvotes: 2
Reputation: 5151
How about using usort()
:
$input = array(
array('date' => '2013-09-11 13:08:40 +0000'),
array('date' => '2013-09-11 13:09:17 +0000'));
usort($input, function(array $a, array $b) {
$aTimestamp = strtotime($a['date']);
$bTimestamp = strtotime($b['date']);
if($aTimestamp == $bTimestamp) return 0;
return $aTimestamp < $bTimestamp;
});
print_r($input); //$input[0] has the latest date value
Upvotes: 1