3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ ['App', 'Classes', 'Auth'], ['App', 'Classes', 'Auth'], ['App', 'Classes', 'Middleware'], ['App', 'Classes', 'Phone'], ['App', 'Classes', 'Auth'], ['App', 'Mail'], ['App', 'Mail', 'Sender'], ['App', 'Box'], ]; $hierarchy = []; foreach($array as $each_data){ $curr_hierarchy = &$hierarchy; foreach($each_data as $each_namespace){ if(!isset($curr_hierarchy[$each_namespace])){ $curr_hierarchy[$each_namespace] = []; } $curr_hierarchy = &$curr_hierarchy[$each_namespace]; } } print_r($hierarchy);
Output for 7.1.25 - 7.1.28, 7.2.0 - 7.2.33, 7.3.0 - 7.3.26, 7.4.0 - 7.4.16, 8.0.0 - 8.0.3
Array ( [App] => Array ( [Classes] => Array ( [Auth] => Array ( ) [Middleware] => Array ( ) [Phone] => Array ( ) ) [Mail] => Array ( [Sender] => Array ( ) ) [Box] => Array ( ) ) )