Last active
November 4, 2024 11:21
-
-
Save farinspace/1126042 to your computer and use it in GitHub Desktop.
Recursive functions, I can never find exactly what I need in a pinch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function combos($data, &$all = array(), $group = array(), $val = null, $i = 0) { | |
if (isset($val)) { | |
array_push($group, $val); | |
} | |
if ($i >= count($data)) { | |
array_push($all, $group); | |
} else { | |
foreach ($data[$i] as $v) { | |
combos($data, $all, $group, $v, $i + 1); | |
} | |
} | |
return $all; | |
} | |
$data = array( | |
array('a', 'b'), | |
array('e', 'f', 'g'), | |
array('w', 'x', 'y', 'z'), | |
); | |
$combos = combos($data); | |
echo '<pre>'; print_r($combos); echo '</pre>'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array | |
( | |
[0] => Array | |
( | |
[0] => a | |
[1] => e | |
[2] => w | |
) | |
[1] => Array | |
( | |
[0] => a | |
[1] => e | |
[2] => x | |
) | |
[2] => Array | |
( | |
[0] => a | |
[1] => e | |
[2] => y | |
) | |
[3] => Array | |
( | |
[0] => a | |
[1] => e | |
[2] => z | |
) | |
[4] => Array | |
( | |
[0] => a | |
[1] => f | |
[2] => w | |
) | |
[5] => Array | |
( | |
[0] => a | |
[1] => f | |
[2] => x | |
) | |
[6] => Array | |
( | |
[0] => a | |
[1] => f | |
[2] => y | |
) | |
[7] => Array | |
( | |
[0] => a | |
[1] => f | |
[2] => z | |
) | |
[8] => Array | |
( | |
[0] => a | |
[1] => g | |
[2] => w | |
) | |
[9] => Array | |
( | |
[0] => a | |
[1] => g | |
[2] => x | |
) | |
[10] => Array | |
( | |
[0] => a | |
[1] => g | |
[2] => y | |
) | |
[11] => Array | |
( | |
[0] => a | |
[1] => g | |
[2] => z | |
) | |
[12] => Array | |
( | |
[0] => b | |
[1] => e | |
[2] => w | |
) | |
[13] => Array | |
( | |
[0] => b | |
[1] => e | |
[2] => x | |
) | |
[14] => Array | |
( | |
[0] => b | |
[1] => e | |
[2] => y | |
) | |
[15] => Array | |
( | |
[0] => b | |
[1] => e | |
[2] => z | |
) | |
[16] => Array | |
( | |
[0] => b | |
[1] => f | |
[2] => w | |
) | |
[17] => Array | |
( | |
[0] => b | |
[1] => f | |
[2] => x | |
) | |
[18] => Array | |
( | |
[0] => b | |
[1] => f | |
[2] => y | |
) | |
[19] => Array | |
( | |
[0] => b | |
[1] => f | |
[2] => z | |
) | |
[20] => Array | |
( | |
[0] => b | |
[1] => g | |
[2] => w | |
) | |
[21] => Array | |
( | |
[0] => b | |
[1] => g | |
[2] => x | |
) | |
[22] => Array | |
( | |
[0] => b | |
[1] => g | |
[2] => y | |
) | |
[23] => Array | |
( | |
[0] => b | |
[1] => g | |
[2] => z | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function is not useful in case of maintaining the key too. like if input is
$data = array(
array('11'=>'a', '22' => 'b'),
array('33' => 'e', '44' => 'f'),
);
It will not return the key as is.