I have a multidimensional array:
$items = array(
array("A1", "A2", "A3"),
array("B1","B2","B3","B4"),
array("C1","C2","C3","C4","C5"),
array("D1","D2","D3"));
I need to build a list of unique strings, and each should contain one item from each array, like this:
A1 B1 C1 D1,
A1 B1 C1 D2,
A1 B1 C1 D3,
A1 B1 C2 D1,
A1 B1 C2 D2,
etc...
Example data above should produce 180 combinations (3*4*5*3=180).
The difficulty is that the code (in best case) should work for any number of arrays. Not just for 4 as in the example above.
For example, I would add: array("E1", "E2"); and code should still work.
A simpler version might be working with fixed set of arrays, for example 4 (as in the example above).
Of course I can use for loops, but in this case it will be required to add another for loop, each time I add another array of data (not so universal, right?).
Here's what I tried, but it didn't work out:
$items = array(
array("A1", "A2", "A3"),
array("B1","B2","B3","B4"),
array("C1","C2","C3","C4","C5"),
array("D1","D2","D3"));
$depth = count($items);
$index = $depth - 1;
for ($i = 0; $i <= $index; $i++) {
$depth2 = count($items[$i]);
$index2 = $depth2 - 1;
echo "SET: ";
for ($i2 = 0; $i2 <= $index2; $i2++) {
echo $items[$i2][$i].", ";
}
echo "<br>";
}
Expected result is the list of 180 unique strings, like:
A1 B1 C1 D1,
A1 B1 C1 D2,
A1 B1 C1 D3,
A1 B1 C2 D1,
A1 B1 C2 D2,
etc...