All possibilities in 2d array
Posted
by valli-R
on Stack Overflow
See other posts from Stack Overflow
or by valli-R
Published on 2010-05-09T20:57:07Z
Indexed on
2010/05/09
23:58 UTC
Read the original article
Hit count: 141
I have this array:
$array = array
(
array('1', '2', '3'),
array('!', '@'),
array('a', 'b', 'c', 'd'),
);
And I want to know all character combination of sub arrays.. for example :
1!a
1!b
1!c
1!d
1@a
1@b
1@c
1@d
2!a
2!b
2!c
2!d
2@a
2@b
...
Currently I am having this code :
for($i = 0; $i < count($array[0]); $i++)
{
for($j = 0; $j < count($array[1]); $j++)
{
for($k = 0; $k < count($array[2]); $k++)
{
echo $array[0][$i].$array[1][$j].$array[2][$k].'<br/>';
}
}
}
It works, but I think it is ugly, and when I am adding more arrays, I have to add more for. I am pretty sure there is a way to do this recursively, but I don't know how to start/how to do it. A little help could be nice!
Thanks you!
© Stack Overflow or respective owner