3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array ( "100" => array( array( "country" => 'France', "city" => 'Paris', "population" => '1800000', ), array( "country" => 'France', "city" => 'Paris', "population" => '2000000', ), array( "country" => 'France', "city" => 'Toulouse', "population" => '500000', ), ), "101" => array( array( "country" => 'Russia', "population" => '144000000', ) ) ); foreach($arr as $key => $sub){ foreach($sub as $item){ if(isset($item['city'])){ if(!isset($res[$key][$item['country'] . $item['city']])) $res[$key][$item['country'] . $item['city']] = $item; if($res[$key][$item['country'] . $item['city']] < $item['population']) $res[$key][$item['country'] . $item['city']] = $item; }else{ if(!isset($res[$key][$item['country']])) $res[$key][$item['country']] = $item; if($res[$key][$item['country']] < $item['population']) $res[$key][$item['country']] = $item; } } } var_dump($res);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array(2) { [100]=> array(2) { ["FranceParis"]=> array(3) { ["country"]=> string(6) "France" ["city"]=> string(5) "Paris" ["population"]=> string(7) "1800000" } ["FranceToulouse"]=> array(3) { ["country"]=> string(6) "France" ["city"]=> string(8) "Toulouse" ["population"]=> string(6) "500000" } } [101]=> array(1) { ["Russia"]=> array(2) { ["country"]=> string(6) "Russia" ["population"]=> string(9) "144000000" } } }

preferences:
181.37 ms | 404 KiB | 213 Q