2

I've ran into a problem while creating a script on PHP. I have 3 arrays styles, colors and sizes (this is the order). I need to combine values between them to get something like a Cartesian Product. 3 imbricated foreachs should help, but any (or even all) can be empty. How to combine them corectly?

Ex:

$styles = array('formal', 'dressy');
$colors = array();
$sizes = array(16, 18);

The result should be:

$res = array(
    array('style' => 'formal', 'size' => 16),
    array('style' => 'formal', 'size' => 18),
    array('style' => 'dressy', 'size' => 16),
    array('style' => 'dressy', 'size' => 18),
);

If $colors weren't empty, then between style and size should be the color. How can I achieve this?

Marin Bînzari
  • 5,208
  • 2
  • 26
  • 43

0 Answers0