Skip to content

Instantly share code, notes, and snippets.

@caferrari
Created February 5, 2014 12:52
Show Gist options
  • Save caferrari/8822983 to your computer and use it in GitHub Desktop.
Save caferrari/8822983 to your computer and use it in GitHub Desktop.
<?php
function permutate($array) {
$current = array_shift($array);
$result = array();
if ($array) {
foreach (permutate($array) as $permutation) {
foreach ($current as $value) {
$result[] = array_merge(array($value), $permutation);
}
}
} else {
foreach ($current as $value) {
$result[] = array($value);
}
}
return $result;
}
$input = array(
array('asus', 'gigabyte'),
array('onboard', 'radeon', 'nvidia'),
array('kingston', 'mushkin', 'corsair'),
array('samsung', 'western digital', 'seagate')
);
$result = permutate($input);
echo json_encode($result, JSON_PRETTY_PRINT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment