3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $root = array( 1=>'cat1', 2=>'cat2', 3=>'cat3' ); $child_1 = array( 1=>'sub_cat1', 2=>'sub_cat2', 3=>'sub_cat3' ); $child_2 = array( 1=>'sub_sub_cat1', 2=>'sub_sub_cat2', 3=>'sub_sub_cat3' ); function getAllCombinationsArray($source_arrays = array(), $kd = array(), $vd = array()){ $return = array(); foreach(array_shift($source_arrays) as $ck => $cv) { $kdi = $kd; array_push($kdi, $ck); $vdi = $vd; array_push($vdi, $cv); if(!empty($source_arrays)) { $return += getAllCombinationsArray($source_arrays, $kdi, $vdi); } else { $return[implode('_', $kdi)] = implode(' -> ', $vdi); } } return $return; } print_r(getAllCombinationsArray(array($root, $child_1, $child_2)));
based on 7RbtB
Output for 4.3.0 - 7.1.0
Array ( [1_1_1] => cat1 -> sub_cat1 -> sub_sub_cat1 [1_1_2] => cat1 -> sub_cat1 -> sub_sub_cat2 [1_1_3] => cat1 -> sub_cat1 -> sub_sub_cat3 [1_2_1] => cat1 -> sub_cat2 -> sub_sub_cat1 [1_2_2] => cat1 -> sub_cat2 -> sub_sub_cat2 [1_2_3] => cat1 -> sub_cat2 -> sub_sub_cat3 [1_3_1] => cat1 -> sub_cat3 -> sub_sub_cat1 [1_3_2] => cat1 -> sub_cat3 -> sub_sub_cat2 [1_3_3] => cat1 -> sub_cat3 -> sub_sub_cat3 [2_1_1] => cat2 -> sub_cat1 -> sub_sub_cat1 [2_1_2] => cat2 -> sub_cat1 -> sub_sub_cat2 [2_1_3] => cat2 -> sub_cat1 -> sub_sub_cat3 [2_2_1] => cat2 -> sub_cat2 -> sub_sub_cat1 [2_2_2] => cat2 -> sub_cat2 -> sub_sub_cat2 [2_2_3] => cat2 -> sub_cat2 -> sub_sub_cat3 [2_3_1] => cat2 -> sub_cat3 -> sub_sub_cat1 [2_3_2] => cat2 -> sub_cat3 -> sub_sub_cat2 [2_3_3] => cat2 -> sub_cat3 -> sub_sub_cat3 [3_1_1] => cat3 -> sub_cat1 -> sub_sub_cat1 [3_1_2] => cat3 -> sub_cat1 -> sub_sub_cat2 [3_1_3] => cat3 -> sub_cat1 -> sub_sub_cat3 [3_2_1] => cat3 -> sub_cat2 -> sub_sub_cat1 [3_2_2] => cat3 -> sub_cat2 -> sub_sub_cat2 [3_2_3] => cat3 -> sub_cat2 -> sub_sub_cat3 [3_3_1] => cat3 -> sub_cat3 -> sub_sub_cat1 [3_3_2] => cat3 -> sub_cat3 -> sub_sub_cat2 [3_3_3] => cat3 -> sub_cat3 -> sub_sub_cat3 )