I need help looping through a simple array and find all possible combination of its elements like this:
array('a', 'b', 'c');
Expected result is:
array(
0=>array('a', 'a', 'a'),
1=> array('a', 'a', 'b')
);
I'm at a complete loss and any help or pointers is much appreciated!
This is what I have so far.
$array = array('red', 'blue', 'green', 'white');
$count = count($array);
$current = array_fill(0, $count, $array[0]);
$last = array_fill(0, $count, end($array));
$output = array();
$i = 0;
while ($current != $last) {
$indexes = str_pad($i, $count, "0", STR_PAD_LEFT);
$j = str_split($indexes);
foreach ($j as $a => $b) {
if (isset($array[$b])) {
$output[$i][] = $array[$b];
}
}
$current = $output[$i];
$i++;
}
// cleaver duplication removal
$result = array_map("unserialize", array_unique(array_map("serialize", $output)));
echo '<pre>';
print_r($result);
echo '</pre>';
Duplicate removal code is from here.