3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ "2022-10-30" => ['▶'], "2022-10-31" => ['▶'], "2022-11-07" => ['▶'], "2022-11-09" => ['▶'] ]; $result = []; foreach ($array as $key => $value) { $ym = substr($key, 0, 7); if (!isset($lookup[$ym]) || $lookup[$ym] < $key) { $result[$key] = $value; if (isset($lookup[$ym])) { unset($result[$lookup[$ym]]); } $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:
114.11 ms | 402 KiB | 121 Q