1

given this example below,

//Array A

$arrayA = [
   'a',
   'b',
   'c'
];

//Array B

$arrayB = [
   'x',
   'y',
   'z'
];

how to loop those two arrays if i want to have the final combined array like this ?

$combinedArray = [
     ['a','x'],
     ['a','y'],
     ['a','z'],
     ['b','x'],
     ['b','y'],
     ['b','z'],
     ['c','x'],
     ['c','y'],
     ['c','z']
];

as you can see the output above, each element of Array A has an element of Array B

here's my solution

$combined = [];
foreach ($arrayA as $a) {
 for ($i=0; $i < sizeof($arrayB); $i++) {
       $combined[] = [
          $a,
          $arrayB[$i]
        ];
   }
}
miken32
  • 39,644
  • 15
  • 91
  • 133
sasori
  • 4,957
  • 16
  • 80
  • 128

4 Answers4

1

First Iterate over $arrayA, then inside that loop, iterate over $arrayB. Inside the loop of $arrayB, create your $combinedArray key/value pair and push it:

$combinedArray= [];
foreach($arrayA as $a){
  foreach($arrayB as $b) {
  $combinedArray[] = [$a,$b];
 }
  
}

A JS POC:

const arrayA = [
   'a',
   'b',
   'c'
];

//Array B

const arrayB = [
   'x',
   'y',
   'z'
];

combinedArray = [];

arrayA.forEach((a)=>{
   arrayB.forEach((b)=>{
     console.log(`["${a}","${b}"]`);
     combinedArray.push([a,b]);
   })
});

   // console.log(combinedArray);
Metabolic
  • 2,589
  • 3
  • 24
  • 38
  • This doesn't produce the desired output? It creates `$key=>$value` pairs and pushes them to an array... The output would look something like `$combinedArray = [0 => ["a"=>"x"], 1 => ["a"=>"y"], ...]` not `$combinedArray = [["a", "x"], ["a", "y"]...]` – Steven Oct 24 '20 at 00:22
  • @Steven check updated answer, you just need to change the way you add the variables to the combinedArray – Metabolic Oct 24 '20 at 01:43
  • @Steven also your original question asked for ` ['b' => 'y']` to be fair – Metabolic Oct 24 '20 at 01:44
  • 1
    It's not my question! But I hadn't looked at the original question, have now checked and you are right it was edited. – Steven Oct 24 '20 at 01:50
0

The most straightforward option would be to just utilise foreach for a second time.

$arrayA = ['a','b','c'];

$arrayB = ['x','y','z'];

$combined = []; // Ready for output
foreach($arrayA as $a){ // Loop through elements in first array
    foreach($arrayB as $b){ // Loop through elements in second array
        $combined[] = [$a, $b]; // Add combination to output array
    }
}

// OUTPUT
// $combined = [["a","x"],["a","y"],["a","z"],["b","x"],["b","y"],["b","z"],["c","x"],["c","y"],["c","z"]];
Steven
  • 5,978
  • 2
  • 14
  • 28
0

This will do the work :

$arrayA = [
   'a',
   'b',
   'c'
];

//Array B

$arrayB = [
   'x',
   'y',
   'z'
];
$combinedArray = [array()];
$count =0;
for($i=0;$i<count($arrayA);$i++){
    for($e=0;$e<count($arrayB);$e++){
        $combinedArray[$count] = [$arrayA[$i],$arrayB[$e]];
        $count++;
    }
}
print_r($combinedArray);
Alaa Kaddour
  • 173
  • 1
  • 6
0

This can also be done using the standard array functions of PHP.

$arrayA = [ 'a','b','c' ];

$arrayB = [ 'x','y','z' ];

$combined = [];

 foreach ($arrayA as $item ) {
    $temp =  array_map( function ( $a, $b ){
       return [ $a, $b ]; 
    } , array_fill( 0, \count( $arrayB ), $item ) , $arrayB);   

    $combined = array_merge( $combined, $temp );
 }
var_dump($combined);
Al-Amin
  • 526
  • 3
  • 15