2

Let's assume I have an two arrays and I want to merge every value with the other value of the array.

Array 1

array (size=2)
  0 => 1
  1 => 2

Array 2

array (size=2)
  0 => 3
  1 => 4

Wanted result array / string:

array (size=4)
  0 => '1,3'
  1 => '1,4'
  2 => '2,3'
  3 => '2,4'

I can't get my head around it. Obviously I would need to merge every one array key/value with the other ones. Is there a more elegant way then doing this in a while/foreach loop?

s1x
  • 574
  • 4
  • 15
  • so what you have tried? – Rakesh Sharma Oct 31 '14 at 12:56
  • 1
    I thnk the title concantenate values of two arrays. Would be more suitable. – Rimble Oct 31 '14 at 13:02
  • Concatenate*, apologies. @s1x – Rimble Oct 31 '14 at 13:05
  • Thanks. It's sometimes really difficult to describe your problem in words, especially if you're not sure what your problem is :-). – s1x Oct 31 '14 at 13:08
  • 1
    No problem at all. When you mention `merging` people usually assume `array_merge()` which is basicly extending one array with the other. Concatenate is basicly pasting pieces of data together. Which is what you wanted. Trying to properly formulate what you tried and what you want helps people help you! – Rimble Oct 31 '14 at 13:10

3 Answers3

2

You need a foreach loop inside a foreach loop. (Actualy, you will have to loop through both arrays to get a concatenated product of both arrays, you don't actually need two foreach loops). You could mix: whiles, foreach, for, or php filter/intersect array functions

Example

$array1 = array(1,2);
$array2 = array(3,4);
$result = array();

foreach ($array1 as $item1){
   foreach($array2 as $item2){
      $result[] = $item1.','.$item2;
   }
}

https://eval.in/215001

your result array Length will be array1.Length * array2.Length

2d arrays

You could also put an array inside an array like this:

$array1 = array(1,2);
$array2 = array(3,4);
$result = array();

foreach ($array1 as $item1){
   foreach($array2 as $item2){
      $result[] = array($item1, $item2);
   }
}
//$result[0][0] = 1 -- $result[0][1] = 3
//$result[1][0] = 1 -- $result[1][1] = 4
//$result[2][0] = 2 -- $result[2][1] = 3
//$result[3][0] = 2 -- $result[3][1] = 4

We call this a 2d (2 dimensional) array, because you could grapicly display this as a grid, like displayed here above. If you would put an Array, inside an Array inside an Array, you would call this a 3 dimensional array, etc.

print_r($result); in php:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 3
        )

    [1] => Array
        (
            [0] => 1
            [1] => 4
        )

    [2] => Array
        (
            [0] => 2
            [1] => 3
        )

    [3] => Array
        (
            [0] => 2
            [1] => 4
        )

)
Community
  • 1
  • 1
Joel Harkes
  • 10,975
  • 3
  • 46
  • 65
1

try

$a= array ('0' => 1,'1' => 2);
$b= array ('0' => 3,'1' => 4);
for($i=0; $i<count($a); $i++) {
  for($j=0; $j<count($b); $j++) {
    $newarr[]= $a[$i].','.$b[$j];
  }
}
print_r($newarr);//Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 ) 
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
1
$a=array('1','2');
$b=array('3','4');
$res=array();
for($i=0;$i<count($a);$i++)
{
    foreach($b as $bb)
    {
        $res[]=strval($a[$i].','.$bb);
    }


}
print_r($res);//output=Array ( [0] => 1,3 [1] => 1,4 [2] => 2,3 [3] => 2,4 ) 
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
  • @Rakesh Sharma $bb is variable that hold the value of $b check this http://php.net/manual/en/control-structures.foreach.php – Ankur Bhadania Oct 31 '14 at 13:06