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

Discuss coding issues, and scripts related to PHP and MySQL.
MarPlo
Posts: 90

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

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.

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); 

Similar Topics