Reply to Re: Get unique combos from arrays

Your name:

Reply:


Posted by Toby A Inkster on 10/11/91 12:00

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-allamatriciana/

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация