Get unique combos from arrays
Get unique combos from arrays
am 16.01.2008 18:04:26 von SBerry
I have three arrays... for instance
$a = array('big', 'small', 'medium');
$b = array('old', 'new');
$c = array('blue', 'green');
I want to take those and end up with all of the combinations they
create like the following
'big', 'old', 'blue'
'small', 'old', 'blue'
'medium', 'old', 'blue'
'big', 'old', 'green'
'small', 'old', 'green'
'medium', 'small', 'green'
'big', 'new', 'blue'
'small', 'new', 'blue'
'medium', 'new', 'blue'
'big', 'new', 'green'
'small', 'new', 'green'
'medium', 'new', 'green'
Is there an easy way to do this mapping?
Re: Get unique combos from arrays
am 16.01.2008 18:27:25 von zeldorblat
On Jan 16, 12:04 pm, sberry wrote:
> I have three arrays... for instance
>
> $a = array('big', 'small', 'medium');
> $b = array('old', 'new');
> $c = array('blue', 'green');
>
> I want to take those and end up with all of the combinations they
> create like the following
> 'big', 'old', 'blue'
> 'small', 'old', 'blue'
> 'medium', 'old', 'blue'
> 'big', 'old', 'green'
> 'small', 'old', 'green'
> 'medium', 'small', 'green'
> 'big', 'new', 'blue'
> 'small', 'new', 'blue'
> 'medium', 'new', 'blue'
> 'big', 'new', 'green'
> 'small', 'new', 'green'
> 'medium', 'new', 'green'
>
> Is there an easy way to do this mapping?
foreach($a as $va) {
foreach($b as $vb) {
foreach($c as $vc) {
echo "'$va', '$vb', '$vc'\n";
}
}
}
Re: Get unique combos from arrays
am 16.01.2008 19:04:51 von Toby A Inkster
sberry wrote:
> $a = array('big', 'small', 'medium');
> $b = array('old', 'new');
> $c = array('blue', 'green');
>
> I want to take those and end up with all of the combinations they create
> like the following
The previews of Perl6 are delicious in this area.
@a = 'big', 'small', 'medium';
@b = 'old', 'new';
@c = 'blue', 'green';
@combos = @a X,X @b X,X @c;
And there, @combos is a list of arrays, with each of the arrays being
something like ('big', 'new', 'blue'). It's got really great functions for
operating on whole data structures in one fell swoop.
Back to the real world of PHP 5 though. The easiest way is to use a
handful of well-placed foreach loops:
$a = array('big', 'small', 'medium');
$b = array('old', 'new');
$c = array('blue', 'green');
$combos = array();
foreach ($a as $x) foreach ($b as $y) foreach ($c as $z)
{
$combos[] = array($x, $y, $z);
}
print_r($combos);
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 17 days, 5:00.]
Gnocchi all'Amatriciana al Forno
http://tobyinkster.co.uk/blog/2008/01/15/gnocchi-allamatrici ana/
Re: Get unique combos from arrays
am 16.01.2008 20:40:59 von SBerry
On Jan 16, 10:04 am, Toby A Inkster
wrote:
> sberry wrote:
> > $a = array('big', 'small', 'medium');
> > $b = array('old', 'new');
> > $c = array('blue', 'green');
>
> > I want to take those and end up with all of the combinations they create
> > like the following
>
> The previews of Perl6 are delicious in this area.
>
> @a = 'big', 'small', 'medium';
> @b = 'old', 'new';
> @c = 'blue', 'green';
>
> @combos = @a X,X @b X,X @c;
>
> And there, @combos is a list of arrays, with each of the arrays being
> something like ('big', 'new', 'blue'). It's got really great functions for
> operating on whole data structures in one fell swoop.
>
> Back to the real world of PHP 5 though. The easiest way is to use a
> handful of well-placed foreach loops:
>
> $a = array('big', 'small', 'medium');
> $b = array('old', 'new');
> $c = array('blue', 'green');
> $combos = array();
>
> foreach ($a as $x) foreach ($b as $y) foreach ($c as $z)
> {
> $combos[] = array($x, $y, $z);
> }
>
> print_r($combos);
>
> --
> Toby A Inkster BSc (Hons) ARCS
> [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
> [OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 17 days, 5:00.]
>
> Gnocchi all'Amatriciana al Forno
> http://tobyinkster.co.uk/blog/2008/01/15/gnocchi-allamatrici ana/
ZeldorBlat:
Thanks for the info. I was trying to do something other than nested
foreach loops, but that seems to be the PHP way to do it.
Toby:
Perl6 does indeed seem to have some really nice features... but
project was developed in PHP (not my choice).
I am used to Python which has a very elegant solution as well using
"List Comprehensions" like so:
a = ['big', 'small', 'medium']
b = ['old', 'new']
c = ['blue', 'green']
d = [[i,j,k] for i in a for j in b for k in c]
print d
[['big', 'old', 'blue'], ['big', 'old', 'green'], ['big', 'new',
'blue'], ['big', 'new', 'green'], ['small', 'old', 'blue'], ['small',
'old', 'green'], ['small', 'new', 'blue'], ['small', 'new', 'green'],
['medium', 'old', 'blue'], ['medium', 'old', 'green'], ['medium',
'new', 'blue'], ['medium', 'new', 'green']]
:)