Get unique combos from arrays

This is a discussion on Get unique combos from arrays within the PHP Language forums, part of the PHP Programming Forums category; I have three arrays... for instance $a = array('big', 'small', 'medium'); $b = array('old', 'new'); $c = array('blue', 'green'); I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-16-2008
sberry
 
Posts: n/a
Default Get unique combos from arrays

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?

Reply With Quote
  #2 (permalink)  
Old 01-16-2008
ZeldorBlat
 
Posts: n/a
Default Re: Get unique combos from arrays

On Jan 16, 12:04 pm, sberry <hacker.steven...@gmail.com> 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";
}
}
}
Reply With Quote
  #3 (permalink)  
Old 01-16-2008
Toby A Inkster
 
Posts: n/a
Default Re: Get unique combos from arrays

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/0...llamatriciana/
Reply With Quote
  #4 (permalink)  
Old 01-16-2008
sberry
 
Posts: n/a
Default Re: Get unique combos from arrays

On Jan 16, 10:04 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
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/0...llamatriciana/



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']]

:)

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:47 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0