Page 1 of 1

Combine items of an array with all the items of another array

Posted: 02 Jan 2015, 15:03
by MarPlo
Hello,
I have two array one is 1 to 8 number of cards and four colors of cards.

Code: Select all

$no = array(1, 2, 3, 4, 5, 6, 7, 8);
$color = array('K','L','F','C');
And i want all the two combination of number to color, the output is like following:

Code: Select all

Array(
  1-K
  2-K
  3-K
  ...
  1-F
  2-F
  3-F
  ...
)
Thanks in advance.

Combine items of an array with all the items of another array

Posted: 02 Jan 2015, 15:10
by Admin
Hi,
Use nested foreach() loops:

Code: Select all

$no = array(1, 2, 3, 4, 5, 6, 7, 8);
$color = array('K','L','F','C');
$result = array();
foreach ($no as $n) {
  foreach ($color as $c) {
    $result[] = $n .'-'. $c;
  }
}
var_export($result);