This is my first question here 🙂
I have an array with a number of array children, each with unique values and would like to get all the possible unique combinations of those values.
The number of arrays is known but may change over time.
For example,
array(
[0] => array([0]=>'blue',[1]=>'red'),
[1] => array([0]=>'sunny',[1]=>'cloudy'),
[2] => array([0]=>'sweet',[1]=>'acid');
What should I do to get:
array(
[0] => array([0]=>'blue',[1]=>'sunny',[2]=>'sweet'),
[1] => array([0]=>'blue',[1]=>'sunny',[2]=>'acid'),
[2] => array([0]=>'blue',[1]=>'cloudy',[2]=>'sweet'),
[3] => array([0]=>'blue',[1]=>'cloudy',[2]=>'acid'),
[4] => array([0]=>'red',[1]=>'sunny',[2]=>'sweet'),
[5] => array([0]=>'red',[1]=>'sunny',[2]=>'acid'),
[6] => array([0]=>'red',[1]=>'cloudy',[2]=>'sweet'),
[7] => array([0]=>'red',[1]=>'cloudy',[2]=>'acid'));
I’ve tried doing it with nested loops but my logic is not too strong.
Very much appreciated if someone can shed some light
(Note: needs a slight modification to use in PHP < 5.3)
Do this (example on an online interpreter):
The function
array_outer, inspired in Mathematica’sOuter, is this: