0

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 :)

2
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by cereal
2

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 :)

Votes + Comments
excellent answer...
Nice - couldn't get my head around this one - like the array_merge_recursive
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.