-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.

Ketan
  • 79
  • 1
  • 9

1 Answers1

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

Oliver A.
  • 2,870
  • 2
  • 19
  • 21