473,889 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Jan 16 '08 #1
3 2157
On Jan 16, 12:04 pm, sberry <hacker.steven. ..@gmail.comwro te:
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";
}
}
}
Jan 16 '08 #2
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/
Jan 16 '08 #3
On Jan 16, 10:04 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
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']]

:)

Jan 16 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2069
by: Keith | last post by:
A2003, Xp Pro. I've designed a form which contains 6 of combos. Three on the left are set up to select fields from a query, three on the right are set up to select values from the corresponding field selected on the left. I use these combos to dynamically create a filter string to apply to my main form. It all works fine as long as I select text fields using quotation marks. My question is how do I handle different data types such...
0
1122
by: Eric Eggermann | last post by:
Hello, I'm rather a fan of creating classes derived from a combo box with one specific functionality like listing installed printers and whatnot. I call the code to load the combos from the constructor. My problem is that the Windows forms designer always adds a .Items.AddRange to the code with all the items that should be there when running on my machine. How can I get the Forms designer to stop doing that? Eric Eggermann
3
1193
by: BrianDH | last post by:
Hi I have a form with multi-tabs, and on each tab I have a one combo that has the same values. I populate all 3 combo with the same one datacall/dataset My problem is: when I make a value selection change to one, it changes the other 2 combos on the other 2 tabs. Is there a way to make one datacall, but have three seperate acting controls?
5
28410
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through each element but I am having issues using redim to adjust the new array. Any help or example code would be greatly appreciated. thanks!
1
1101
by: G .Net | last post by:
Hi I have a DataGrid which has combos in the first field. These combos are populated via a DataView from another table. When I click the header of the field i.e. to sort it, the rows are sorted via the foriegn key in the combo, rather than the value that is actually displayed. How can I get the sort to use the value displayed in the combo? G
2
1566
by: Gordowey | last post by:
Hi all,...I have an special situation, that I can not solve.... I need some help This is my scenario: 1.- I have n-combos (showing available rooms from an hotel) in my web-page. The number of combos is not fixed. 2.- Some of the combos are dependent. I mean:
11
1338
by: Zebra Code | last post by:
hi all i should copy the items of a combo to n other combos. it's not a cycle: With cboColor1 .Items.Add("ffffff") .Items.Add("cecece") ... .SelectedIndex = 0
3
1474
by: ilikebirds | last post by:
I am looking for a way to have one combo lookup drop down to autopopulate with multiple items based off another combo lookup. I've tried Me!rc1 = DLookup("", "", "Reject_Reason_1='" & Me!Reject_Reason_1 & "'") But it only gives me one of the possible items. For Instance: Table xRoot_cause:
5
3603
by: macca | last post by:
Hi, I'm doing an two ldap_search queries and I need to combine the two results into one single array containing all the results from each but removing duplicates. I have tried built in php functions such as array_merge (which gives me duplicates) and array_unique which does not work either.
1
10895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9611
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4650
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3256
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.