EBuzila
EBuzila

Reputation: 229

Combine a value from an array with each values from other?

What i want is to combine every value from first array with every value from second array. For example, let's take two arrays:

$array1 = ['green', 'red', 'blue'];
$array2 = ['s', 'm'];

The result array should be:

$result = [1 => 'green-s', 2 => 'green-m', 3 => 'red-s', 4 => 'red-m', 5 => 'blue-s' ...];

The result array can be different, but with that elements combined.

Upvotes: 0

Views: 51

Answers (1)

Gayan
Gayan

Reputation: 2935

check this,

<?php

$array1 = array('green', 'red', 'blue');
$array2 = array('s', 'm');

$data = array();
foreach($array1 as $val){
    foreach($array2 as $val2){
        $data[] = $val."-".$val2;
    }
}

print_r($data);

?>

Upvotes: 1

Related Questions