Created
October 21, 2018 22:59
-
-
Save mrself/abaf723a79ccc90527f60f4879ddb2e0 to your computer and use it in GitHub Desktop.
PHP make combination array
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
function getCombination($input, $carry, &$result, $maxLength = null) | |
{ | |
$maxLength = $maxLength ?: count($input); | |
$oldCarry = $carry; | |
foreach ($input as $index => $i) { | |
$carry[] = $i; | |
if (count($carry) <= $maxLength) { | |
$result[] = $carry; | |
} | |
getCombination(array_slice($input, $index + 1), $carry, $result, $maxLength); | |
$carry = $oldCarry; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment