hi all,
i have two three dimensional array like this

$array1 =Array
        (
         [0]= array
              (
                  [0]=array
                       (
                        [name]= praveen
                        [id]  = 20
                       )
                  [1]=array
                       (
                        [name]= kumar
                        [id]  = 25
                       )

                )

          )

$array2 =Array
        (
         [0]= array
              (
                  [0]=array
                       (
                        [name]= pandu
                        [id]  = 21
                       )
                  [1]=array
                       (
                        [name]= praveen
                        [id]  = 30
                       )

                )

          )

i want to merge these arrays such that i will get an array like this

$merged= array
        (
          [0]= array
              (
                  [praveen]=array
                       (
                        [id1]= 20
                        [id2]  = 30
                       )
                  [pandu]=array
                       (
                        [id]  = 21
                       )
                   [kumar]=array
                       (
                        [id]  = 25
                       )

                )


          )

i tried this a lot but not getting it right...if someone solves it will be great :)

You can get something similar to your desired output with this script:

<?php
$a = array(array(array('name' => 'praveen','id' => '20'),array('name' => 'kumar','id' => '25')));
$b = array(array(array('name' => 'pandu','id' => '21'),array('name' => 'praveen','id' => '30')));
$c = array_merge($a,$b);
$arr = count($c);

for($i = 0; $i < $arr; $i++)
{
	foreach($a as $key => $value)
	{
	    $q1[$value[$i]['name']] = array('id' => $value[$i]['id']);
	}
	
	foreach($b as $key => $value)
	{
            $q2[$value[$i]['name']] = array('id' => $value[$i]['id']);
	}
}
print_r(array_merge_recursive($q1,$q2));
?>

The output will be:

Array
(
    [praveen] => Array
        (
            [id] => Array
                (
                    [0] => 20
                    [1] => 30
                )
        )

    [kumar] => Array
        (
            [id] => 25
        )

    [pandu] => Array
        (
            [id] => 21
        )
)

As you can see, instead of id1, id2, you will get an id array. Bye :)

commented: Nice - couldn't get my head around this one - like the array_merge_recursive +13
commented: excellent answer... +5

You can get something similar to your desired output with this script:

<?php
$a = array(array(array('name' => 'praveen','id' => '20'),array('name' => 'kumar','id' => '25')));
$b = array(array(array('name' => 'pandu','id' => '21'),array('name' => 'praveen','id' => '30')));
$c = array_merge($a,$b);
$arr = count($c);

for($i = 0; $i < $arr; $i++)
{
	foreach($a as $key => $value)
	{
	    $q1[$value[$i]['name']] = array('id' => $value[$i]['id']);
	}
	
	foreach($b as $key => $value)
	{
            $q2[$value[$i]['name']] = array('id' => $value[$i]['id']);
	}
}
print_r(array_merge_recursive($q1,$q2));
?>

The output will be:

Array
(
    [praveen] => Array
        (
            [id] => Array
                (
                    [0] => 20
                    [1] => 30
                )
        )

    [kumar] => Array
        (
            [id] => 25
        )

    [pandu] => Array
        (
            [id] => 21
        )
)

As you can see, instead of id1, id2, you will get an id array. Bye :)

hi cereal,
Excellent!!!!!! my problem is solved....thank you so much...

Regards,
Praveen

You're welcome!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.