3v4l.org

run code in 300+ PHP versions simultaneously
<?php $levels = ['country','state','city','location']; $locations = [ ['country'=>'USA', 'state'=>'New York', 'city'=>'NYC', 'location'=>'Central Park', 'street'=>'7th Ave', 'count'=>123], ['country'=>'USA', 'state'=>'Maryland', 'city'=>'Baltimore', 'location'=>'Harber', 'count'=>24], ['country'=>'USA', 'state'=>'Michigan', 'city'=>'Lansing', 'location'=>'Midtown', 'building'=>'H2B', 'count'=>7], ['country'=>'France', 'state'=>'Sud', 'city'=>'Marseille', 'location'=>'Centre Ville', 'count'=>12], ]; $output = []; foreach($locations as $loc){ $str_to_eval='$output'; for($i=0;$i<count($levels);$i++){ $str_to_eval .= "[\$loc[\$levels[$i]]]"; } $str_to_eval .= "=\$loc['count'];"; eval($str_to_eval); } print_r($output);

preferences:
73.53 ms | 404 KiB | 5 Q