-4

I am stuck at one place where I have 3 arrays as below:

$c = array("red","green","blue");
$s = array("12","13","14");
$l = array("3","4","5");

Based on this array I need arrays like:

$one = array("red", "12", "3");
$two = array("red", "12", "4");
$three = array("red", "12", "5");
$three = array("red", "13", "3");
...........

Same I need to create array which include all combinations of above 3 arrays.

I have tried to create by looping through all, but can anyone tell me a better solution or existing php function for this.

2
  • The "looping through all" solution should only be about 4 lines of code, why are you looking for something "better"? Commented Apr 2, 2013 at 12:42
  • Thanks @deceze, it is better solution than I have made Commented Apr 2, 2013 at 12:46

1 Answer 1

0

There is a helper class for that:

https://github.com/noptic/co3/blob/master/src/daliaIT/co3/util/generator/ArrayGenerator.php

The package is no longer mantained but you can remove the 'IInject' interface and the "inject" method and use it as a standalone class.

The class can be iterated with foreach without creating all the combinations in memory.

If you need a "real" array call the "toArray" method

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.