Reputation: 10068
If I have the following array how can I get the max expiry date? The expiry date is always the value in the values array with key 5 i.e. $results['values']['5']
array (
0 =>
array (
'values' =>
array (
1 => '123456',
2 => 'Blue',
3 => 'Container',
4 => '03-01-2020', // start date
5 => '03-01-2021', // exp date
6 => '',
7 => '',
),
),
1 =>
array (
'values' =>
array (
1 => '21312',
2 => 'Green',
3 => 'Box',
4 => '2019-04-12', // start date
5 => '', //exp date
6 => '',
7 => '',
),
),
2 =>
array (
'values' =>
array (
1 => '434324',
2 => 'Orange',
3 => 'Box',
4 => '2018-04-28', // start date
5 => '2019-04-23', // exp date
6 => '',
7 => '',
),
),
)
I know I can do something as follows for a simple array:
$max = max(array_map('strtotime', $arr));
echo date('d F Y', $max);
How can I achieve the same with a nested array?
Upvotes: 0
Views: 910
Reputation: 14927
Since you're already using array_map
, you may use a custom function that grabs the expiry date and uses strtotime
on it in one go:
$expiryDates = array_map(static function ($entry) {
return strtotime($entry['values'][5]);
}, $input);
echo date('d F Y', max($expiryDates));
Demo: https://3v4l.org/AiFtA
Bonus - using PHP 7.4 short closures:
$expiryDates = array_map(fn($entry) => strtotime($entry['values'][5]), $input);
echo date('d F Y', max($expiryDates));
Upvotes: 3