9

I have 3 arrays as below.

$array1 = Array
(
    [0] => 05/01
    [1] => 05/02
)

$array2 =Array
(
    [0] => ED
    [1] => P
)

$array3 =Array
(
    [0] => Mon
    [1] => Tue

)

I want to merge these 3 arrays as below $result_array. I have written a code as below. But It gave a empty array.

$result_array =Array
(
    [0] => Array
        (
            [0] => 05/01
            [1] => ED
            [2] => Mon
        )
    [1] => Array
        (
            [0] => 05/02
            [1] => P
            [2] => Tue
        )
)

Code:

for($z=0; $z<count($array1); $z++){
    $all_array[$z][] = array_merge($array1[$z],$array2[$z] );
    $all_array2[$z] = array_merge($all_array[$z],$array3[$z] );
}

Please help me to do this.

tenten
  • 1,276
  • 2
  • 26
  • 54
  • Check your 3 arrays. 2nd and 3rd both have same variable name. Please recorrect it. – Virb Jul 18 '17 at 11:05
  • Possible duplicate of [Merge multiple arrays from one array](https://stackoverflow.com/questions/13544985/merge-multiple-arrays-from-one-array) – RJParikh Jul 18 '17 at 11:07
  • You need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and others will be encouraged to help you. – Jay Blanchard Jul 18 '17 at 12:39

6 Answers6

14

Simply foreach over the first array and use the index as the key to the other arrays.

foreach ( $array1 as $idx => $val ) {
    $all_array[] = [ $val, $array2[$idx], $array3[$idx] ];
}

Remember this will only work if all 3 arrays are the same length, you might like to check that first

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
2

Create an sample array and push to each array value with respective key

$sample = array();
for($z=0; $z<count($array1); $z++){
    $sample[]=array($array1[$z],$array2[$z],$array3[$z]);
}
print_r($sample);

Out put is

Array ( [0] => Array ( 
               [0] => 05/01 
               [1] => ED 
               [2] => Mon 
        ) 
        [1] => Array ( 
               [0] => 05/02 
               [1] => P 
               [2] => Tue 
        ) 
)
Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31
2

You can simply walk through the first array with a foreach loop then access the corresponding arrays and combine the results into one big array. This will only work if the arrays are the same length so it's worth checking that before to stop any errors. Something like this:

<?php
$arr1 = ['05/01', '05/02'];
$arr2 = ['ED', 'P'];
$arr3 = ['Mon', 'Tue'];

$combined = [];
if (count($arr1) != count($arr2) || count($arr1) != count($arr3))
  die("Array lengths do not match!");

foreach ($arr1 as $key => $val) {
    $combined[] = [$val, $arr2[$key], $arr3[$key]];
}

var_dump($combined);

You can see an eval.in of this working here - https://eval.in/833893

Styphon
  • 10,304
  • 9
  • 52
  • 86
2

this work for n of arrays and dynamic length of array

function mergeArrays(...$arrays)
{

    $length = count($arrays[0]);
    $result = [];
    for ($i=0;$i<$length;$i++)
    {
        $temp = [];
        foreach ($arrays as $array)
            $temp[] = $array[$i];

        $result[] = $temp;
    }

    return $result;

}

$x = mergeArrays(['05/01' , '05/02'] , ['ED' , 'P'] , ['Mon' , 'Tus']);
$y = mergeArrays(['05/01' , '05/02' , 'X'] , ['ED' , 'P' , 'Y'] , ['Mon' , 'Tus' , 'Z'] , ['A' , 'B' , 'C']);

var_dump($x);
var_dump($y);
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
1
function merge($file_name, $titles, $description)
 {
    $result = array();
    foreach($file_name as $key=>$name )
    {
      $result[] = array( 'file_name' => $name, 'title' => $titles[$key], 
     'description' => $description[ $key ] );
    }
    return $result;
}
1
$array1 = Array
(
    '05/01',
    '05/02'
);

    $array2 = Array
(
    'ED',
    'P'
);

$array3 =Array
(
    'Mon',
    'Tue'
);
$result_array = array();
foreach ($array1 as $key=>$val)
{
    $result_array[$key] = array($array1[$key],$array2[$key],$array3[$key]);
}
echo "<PRE>"; print_r($result_array);
Ashok
  • 128
  • 1
  • 8