3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ "2022-10-30" => ['▶'], "2022-10-31" => ['▶'], "2022-11-09" => ['▶'], "2022-11-07" => ['▶'] ]; $result = []; foreach ($array as $key => $value) { $ym = substr($key, 0, 7); if (!isset($lookup[$ym])) { $result[$key] = $value; $lookup[$ym] = $key; } elseif ($lookup[$ym] < $key) { unset($result[$lookup[$ym]]); $result[$key] = $value; $lookup[$ym] = $key; } } var_export($result);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
array ( '2022-10-31' => array ( 0 => '▶', ), '2022-11-09' => array ( 0 => '▶', ), )

preferences:
122.24 ms | 402 KiB | 121 Q