Skip to content

Instantly share code, notes, and snippets.

@cereal-s
Forked from farinspace/combos.php
Last active August 26, 2019 09:26
Show Gist options
  • Save cereal-s/05dc4edac3091856144a78deedf0ca1e to your computer and use it in GitHub Desktop.
Save cereal-s/05dc4edac3091856144a78deedf0ca1e to your computer and use it in GitHub Desktop.
Recursive functions, I can never find exactly what I need in a pinch
<?php
/**
* Generate combinations from multidimensional array
*/
function combos($data, $count, &$all = [], $group = [], $val = null, $i = 0) {
if (null !== $val) {
$group[] = $val;
}
if ($i >= $count) {
$all[] = $group;
}
else
{
for($z = 0; $z < count($data[$i]); $z++){
combos($data, $count, $all, $group, $data[$i][$z], $i + 1);
}
}
return $all;
}
$data = [
// Group A
['a', 'b'],
// Group B
['e', 'f', 'g'],
// Group C
['w', 'x', 'y', 'z'],
// Group D
['j', 'k', 'l']
];
$combos = combos($data, count($data)); // 2 * 3 * 4 * 3 = 72 combinations
print_r($combos);
Array
(
[0] => Array
(
[0] => a
[1] => e
[2] => w
[3] => j
)
[1] => Array
(
[0] => a
[1] => e
[2] => w
[3] => k
)
[2] => Array
(
[0] => a
[1] => e
[2] => w
[3] => l
)
[3] => Array
(
[0] => a
[1] => e
[2] => x
[3] => j
)
[4] => Array
(
[0] => a
[1] => e
[2] => x
[3] => k
)
[5] => Array
(
[0] => a
[1] => e
[2] => x
[3] => l
)
[6] => Array
(
[0] => a
[1] => e
[2] => y
[3] => j
)
[7] => Array
(
[0] => a
[1] => e
[2] => y
[3] => k
)
[8] => Array
(
[0] => a
[1] => e
[2] => y
[3] => l
)
[9] => Array
(
[0] => a
[1] => e
[2] => z
[3] => j
)
[10] => Array
(
[0] => a
[1] => e
[2] => z
[3] => k
)
[11] => Array
(
[0] => a
[1] => e
[2] => z
[3] => l
)
[12] => Array
(
[0] => a
[1] => f
[2] => w
[3] => j
)
[13] => Array
(
[0] => a
[1] => f
[2] => w
[3] => k
)
[14] => Array
(
[0] => a
[1] => f
[2] => w
[3] => l
)
[15] => Array
(
[0] => a
[1] => f
[2] => x
[3] => j
)
[16] => Array
(
[0] => a
[1] => f
[2] => x
[3] => k
)
[17] => Array
(
[0] => a
[1] => f
[2] => x
[3] => l
)
[18] => Array
(
[0] => a
[1] => f
[2] => y
[3] => j
)
[19] => Array
(
[0] => a
[1] => f
[2] => y
[3] => k
)
[20] => Array
(
[0] => a
[1] => f
[2] => y
[3] => l
)
[21] => Array
(
[0] => a
[1] => f
[2] => z
[3] => j
)
[22] => Array
(
[0] => a
[1] => f
[2] => z
[3] => k
)
[23] => Array
(
[0] => a
[1] => f
[2] => z
[3] => l
)
[24] => Array
(
[0] => a
[1] => g
[2] => w
[3] => j
)
[25] => Array
(
[0] => a
[1] => g
[2] => w
[3] => k
)
[26] => Array
(
[0] => a
[1] => g
[2] => w
[3] => l
)
[27] => Array
(
[0] => a
[1] => g
[2] => x
[3] => j
)
[28] => Array
(
[0] => a
[1] => g
[2] => x
[3] => k
)
[29] => Array
(
[0] => a
[1] => g
[2] => x
[3] => l
)
[30] => Array
(
[0] => a
[1] => g
[2] => y
[3] => j
)
[31] => Array
(
[0] => a
[1] => g
[2] => y
[3] => k
)
[32] => Array
(
[0] => a
[1] => g
[2] => y
[3] => l
)
[33] => Array
(
[0] => a
[1] => g
[2] => z
[3] => j
)
[34] => Array
(
[0] => a
[1] => g
[2] => z
[3] => k
)
[35] => Array
(
[0] => a
[1] => g
[2] => z
[3] => l
)
[36] => Array
(
[0] => b
[1] => e
[2] => w
[3] => j
)
[37] => Array
(
[0] => b
[1] => e
[2] => w
[3] => k
)
[38] => Array
(
[0] => b
[1] => e
[2] => w
[3] => l
)
[39] => Array
(
[0] => b
[1] => e
[2] => x
[3] => j
)
[40] => Array
(
[0] => b
[1] => e
[2] => x
[3] => k
)
[41] => Array
(
[0] => b
[1] => e
[2] => x
[3] => l
)
[42] => Array
(
[0] => b
[1] => e
[2] => y
[3] => j
)
[43] => Array
(
[0] => b
[1] => e
[2] => y
[3] => k
)
[44] => Array
(
[0] => b
[1] => e
[2] => y
[3] => l
)
[45] => Array
(
[0] => b
[1] => e
[2] => z
[3] => j
)
[46] => Array
(
[0] => b
[1] => e
[2] => z
[3] => k
)
[47] => Array
(
[0] => b
[1] => e
[2] => z
[3] => l
)
[48] => Array
(
[0] => b
[1] => f
[2] => w
[3] => j
)
[49] => Array
(
[0] => b
[1] => f
[2] => w
[3] => k
)
[50] => Array
(
[0] => b
[1] => f
[2] => w
[3] => l
)
[51] => Array
(
[0] => b
[1] => f
[2] => x
[3] => j
)
[52] => Array
(
[0] => b
[1] => f
[2] => x
[3] => k
)
[53] => Array
(
[0] => b
[1] => f
[2] => x
[3] => l
)
[54] => Array
(
[0] => b
[1] => f
[2] => y
[3] => j
)
[55] => Array
(
[0] => b
[1] => f
[2] => y
[3] => k
)
[56] => Array
(
[0] => b
[1] => f
[2] => y
[3] => l
)
[57] => Array
(
[0] => b
[1] => f
[2] => z
[3] => j
)
[58] => Array
(
[0] => b
[1] => f
[2] => z
[3] => k
)
[59] => Array
(
[0] => b
[1] => f
[2] => z
[3] => l
)
[60] => Array
(
[0] => b
[1] => g
[2] => w
[3] => j
)
[61] => Array
(
[0] => b
[1] => g
[2] => w
[3] => k
)
[62] => Array
(
[0] => b
[1] => g
[2] => w
[3] => l
)
[63] => Array
(
[0] => b
[1] => g
[2] => x
[3] => j
)
[64] => Array
(
[0] => b
[1] => g
[2] => x
[3] => k
)
[65] => Array
(
[0] => b
[1] => g
[2] => x
[3] => l
)
[66] => Array
(
[0] => b
[1] => g
[2] => y
[3] => j
)
[67] => Array
(
[0] => b
[1] => g
[2] => y
[3] => k
)
[68] => Array
(
[0] => b
[1] => g
[2] => y
[3] => l
)
[69] => Array
(
[0] => b
[1] => g
[2] => z
[3] => j
)
[70] => Array
(
[0] => b
[1] => g
[2] => z
[3] => k
)
[71] => Array
(
[0] => b
[1] => g
[2] => z
[3] => l
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment