3v4l.org

run code in 300+ PHP versions simultaneously
<?php $townsJson = <<<JSON [{"id":"AA0","name":"Aalen","region":"Süden","state":"Baden-Württemberg"}, {"id":"AB0","name":"Aschaffenburg","region":"Mitte","state":"Bayern"}, {"id":"AC0","name":"Aachen","region":"Westen","state":"Nordrhein-Westfalen"}, {"id":"WIT","name":"Witten","region":"Westen","state":"Nordrhein-Westfalen"}, {"id":"DN0","name":"Düren","region":"Westen","state":"Nordrhein-Westfalen"}] JSON; $towns = json_decode($townsJson, true); $towns = array_map(function(array $town) { $town['description'] = sprintf( '%s is a town in %s (%s).', $town['name'], $town['state'], $town['region'] ); return $town; }, $towns); $ATowns = array_filter($towns, fn(array $town) => strpos($town['name'], 'A') === 0); $townsByRegion = array_reduce($towns, function(array $found, array $town) { $found[$town['region']][] = $town; return $found; }, []); var_dump($towns, $ATowns, $townsByRegion);

preferences:
91.1 ms | 410 KiB | 5 Q