-1

I have two arrays.

array1 = {0=>{name=>'anju',addr=>'kottayam'},1=>{name=>'anu',addr=>'kochi'}}

and

array2 = {0=>{place=>'south',state=>'kerala'},1=>{place=>'north',state=>'andra'}}

I want to combine these two arrays such that The resulted array should be

array3 ={0=>{name=>'anju',addr=>'kottayam',place=>'south',state=>'kerala'},1=>{name=>'anu',addr=>'kochi',place=>'north',state=>'andra'}}

ie combine arrays on same key positions together.

Khushboo
  • 1,819
  • 1
  • 11
  • 17
Anju
  • 53
  • 5

3 Answers3

1

Another way could be

$a3 = array_map("array_merge",$a1, $a2));

Gaurav
  • 28,447
  • 8
  • 50
  • 80
0

Without trying I would say:

$array3 = array_merge($array1, $array2);

Or

foreach ($array1 as $key => $value)
{
    $array3 = array_merge($array1[$key], $array2[$key]); // You should ask first if isset($array2[$key])
}
K. Weber
  • 2,643
  • 5
  • 45
  • 77
0
$size1 = sizeof($array1);
$size2 = sizeof($array2);

$cnt = $size1;
if($size2>$size1)
{
     $cnt = $size2;
}

$array3 = array();

for($i=0; $i<$cnt; $i++)
{
    if(isset($array1[$i])
    {
       $array3[$i] = $array1[$i];
    } 

    if(isset($array2[$i])
    {
       $array3[$i] = $array2[$i];
    } 
}

METHOD : 2

$size1 = sizeof($array1);
$size2 = sizeof($array2);

$array3 = array();
if($size2>$size1)
{
    $array3 = $array2;
    foreach($array1 as $key=>$arr)
    {
        $array3[$key] = $arr;
    }
}
else
{
    $array3 = $array1;
    foreach($array2 as $key=>$arr)
    {
        $array3[$key] = $arr;
    }
}
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51