php - Recursive foreach loop (I think) -



php - Recursive foreach loop (I think) -

i have multidimensional array these values:

array(2) { [0]=> array(2) { [0]=> string(16) "5" [1]=> string(16) "4" } [1]=> array(3) { [0]=> string(16) "1" [1]=> string(16) "2" [2]=> string(16) "3" } }

for contents of arrays int, more complex values can passed through brackets.

however, number of "sub-arrays" can varies , can little 1 , big php can handle it.

i wish loop through first value of first array, first value of sec array, first value of 3rd array, etc.

array[0][0] -> array[1][0] -> array[2][0] array[0][0] -> array[1][0] -> array[2][1] array[0][0] -> array[1][1] -> array[2][0] array[0][0] -> array[1][1] -> array[2][1] array[0][0] -> array[1][2] -> array[2][0] array[0][0] -> array[1][2] -> array[2][1] array[0][1] -> array[1][0] -> array[2][0] array[0][1] -> array[1][0] -> array[2][1] array[0][1] -> array[1][1] -> array[2][0] array[0][1] -> array[1][1] -> array[2][1] array[0][1] -> array[1][2] -> array[2][0] array[0][1] -> array[1][2] -> array[2][1] // illustration 2 values in each array.

i've been told recursive function trick have no experience them , far manage partial results.

this current code

// $array - multidimensional array above // $ini - current array - ex: $array[0] // $ini - number of main arrays left - ex: count($array) // $result - string houses temporary value // $finalresult - array combines string when there nil left function r ($array, $ini, $int, $result, $finalresult) { ($i = 0; $i < count($array[$ini]); $i++) { $result = $array[$ini][$i] . ',' . $result; if ($int != 0) { $result = $this->r($array, ++$ini, --$int, $result, $finalresult); } else { $finalresult[] = $result; } } homecoming $finalresult; }

this it

array(2) { [0]=> string(22) "2,array" [1]=> string(39) "3,2,array" } // managed @ point values separate but, sadly

the desired outcome of function values displayed in matter:

array(2) { [0]=> string(16) "5, 1" [0]=> string(16) "5, 2" [0]=> string(16) "5, 3" [0]=> string(16) "4, 1" [0]=> string(16) "4, 2" [0]=> string(16) "4, 3" }

any guidance in right direction appreciated.

thanks in advance!

maybe i'm late believe you're asking, no recursion needed still quite memory pig i'm afraid!

function array_combine_subarray_values( array $grouparray ){ // boost available memory //ini_set('memory_limit', '512m'); //count elements in each subarray index $lenarrs = []; foreach ($grouparray $arr) { $lenarrs[] = count($arr); } // total number of distinct cases $m = array_product($lenarrs); // save our arrays' lengths $lenarrskeep=$lenarrs; $reparr = []; // find number of sequential repetitions given index of subarrays foreach ($lenarrs $lenkey => $len) { $repetitions = 1; unset($lenarrs[$lenkey]); foreach ($lenarrs $lenmultiplierkey => $lenmultiplier) { $repetitions *= $lenmultiplier; } $reparr[$lenkey] = $repetitions; } $target=[]; // iterate through possible cases ($i =0;$i<$m;$i++){ //fill each combination/case foreach ($reparr $index => $repetitions) { $target[$i][]=$grouparray[$index][($i/$repetitions)%$lenarrskeep[$index]]; } } homecoming $target; } $grouparr = [ [0, 3, 6], [1, 4 ,7], [2, 5, 8] ]; //result 0 1 2 #1 0 1 2 #2 0 1 5 #3 0 1 8 #4 0 4 2 #5 0 4 5 #6 0 4 8 #7 0 7 2 #8 0 7 5 #9 0 7 8 #10 3 1 2 #11 3 1 5 #12 3 1 8 #13 3 4 2 #14 3 4 5 #15 3 4 8 #16 3 7 2 #17 3 7 5 #18 3 7 8 #19 6 1 2 #20 6 1 5 #21 6 1 8 #22 6 4 2 #23 6 4 5 #24 6 4 8 #25 6 7 2 #26 6 7 5 #27 6 7 8

php arrays recursion foreach

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -