3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function multiKeyIt($array, $numKeys) { $multiKeyed = []; foreach($array as $row) { if(!isset($countFields)) { $countFields = count($row); } $arrRef = &$multiKeyed; $theKey = reset($row); for($k = 0; $k < $numKeys; $k++) { if(!isset($arrRef[$theKey])) { $arrRef[$theKey] = array(); } $arrRef = &$arrRef[$theKey]; $theKey = next($row); } //$arrRef = $countFields > $numKeys + 1 ? array_merge($arrRef, array_slice($row,$numKeys)) : implode(', ', array_slice($row,$numKeys)); $arrRef = array_merge($arrRef, array_slice($row,$numKeys)); } return $multiKeyed; } $arr =[ ['key'=>'val_k1', 'val2', 'key1_2'=>'val_d1_2', 'key2_2'=>'val_d2_2'], ['key'=>'val_k1', 'val3', 'key1_3'=>'val_d1_3', 'key2_3'=>'val_d2_3'], ['key'=>'val_kx1', 'val3', 'key1_3'=>'val_d1_x3', 'key2_3'=>'val_d2_3'], ]; echo "<pre>"; $da = multiKeyIt($arr, 1); echo "\n!da(1)=".print_r($da,true); $da = multiKeyIt($arr, 2); echo "\n!da(2)=".print_r($da,true); $da = multiKeyIt($arr, 3); echo "\n!da(3)=".print_r($da,true); $da = multiKeyIt($arr, 4); echo "\n!da(4)=".print_r($da,true); $da = multiKeyIt($arr, 5); echo "\n!da(5)=".print_r($da,true); $da = multiKeyIt($arr, 6); echo "\n!da(6)=".print_r($da,true); echo "</pre>";
Output for 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.30 - 7.3.0alpha1
<pre> !da(1)=Array ( [val_k1] => Array ( [0] => val2 [key1_2] => val_d1_2 [key2_2] => val_d2_2 [1] => val3 [key1_3] => val_d1_3 [key2_3] => val_d2_3 ) [val_kx1] => Array ( [0] => val3 [key1_3] => val_d1_x3 [key2_3] => val_d2_3 ) ) !da(2)=Array ( [val_k1] => Array ( [val2] => Array ( [key1_2] => val_d1_2 [key2_2] => val_d2_2 ) [val3] => Array ( [key1_3] => val_d1_3 [key2_3] => val_d2_3 ) ) [val_kx1] => Array ( [val3] => Array ( [key1_3] => val_d1_x3 [key2_3] => val_d2_3 ) ) ) !da(3)=Array ( [val_k1] => Array ( [val2] => Array ( [val_d1_2] => Array ( [key2_2] => val_d2_2 ) ) [val3] => Array ( [val_d1_3] => Array ( [key2_3] => val_d2_3 ) ) ) [val_kx1] => Array ( [val3] => Array ( [val_d1_x3] => Array ( [key2_3] => val_d2_3 ) ) ) ) !da(4)=Array ( [val_k1] => Array ( [val2] => Array ( [val_d1_2] => Array ( [val_d2_2] => Array ( ) ) ) [val3] => Array ( [val_d1_3] => Array ( [val_d2_3] => Array ( ) ) ) ) [val_kx1] => Array ( [val3] => Array ( [val_d1_x3] => Array ( [val_d2_3] => Array ( ) ) ) ) ) !da(5)=Array ( [val_k1] => Array ( [val2] => Array ( [val_d1_2] => Array ( [val_d2_2] => Array ( [0] => Array ( ) ) ) ) [val3] => Array ( [val_d1_3] => Array ( [val_d2_3] => Array ( [0] => Array ( ) ) ) ) ) [val_kx1] => Array ( [val3] => Array ( [val_d1_x3] => Array ( [val_d2_3] => Array ( [0] => Array ( ) ) ) ) ) ) !da(6)=Array ( [val_k1] => Array ( [val2] => Array ( [val_d1_2] => Array ( [val_d2_2] => Array ( [0] => Array ( [0] => Array ( ) ) ) ) ) [val3] => Array ( [val_d1_3] => Array ( [val_d2_3] => Array ( [0] => Array ( [0] => Array ( ) ) ) ) ) ) [val_kx1] => Array ( [val3] => Array ( [val_d1_x3] => Array ( [val_d2_3] => Array ( [0] => Array ( [0] => Array ( ) ) ) ) ) ) ) </pre>