-1

I'd like to merge two arrays on same key.

Here's the 1st array :

Array
(
[2052] => Array
    (
        [495] => Array
            (
                [0] => Array
                    (
                        [ID_RI_BELANJA] => 79755
                    )

                [1] => Array
                    (
                        [ID_RI_BELANJA] => 79755
                    )
            )

    )

[4566] => Array
    (
        [488] => Array
            (
                [0] => Array
                    (
                        [ID_RI_BELANJA] => 231610
                    )

                [1] => Array
                    (
                        [ID_RI_BELANJA] => 231610
                    )
            )

    )
)

And this is the 2nd array

Array
(
[2052] => Array
    (
        [495] => Array
            (
                [TOTAL_RI] => 1000000
                [TOTAL_ANGGARAN] => 500000
            )

    )

[4566] => Array
    (
        [488] => Array
            (
                [TOTAL_RI] => 2000000
                [TOTAL_ANGGARAN] => 1000000
            )

    )
)

And i'd like merge that two arrays to be like this :

Array
(
[2052] => Array
    (
        [495] => Array
            (
                [0] => Array
                    (
                        [ID_RI_BELANJA] => 79755
                    )

                [1] => Array
                    (
                        [ID_RI_BELANJA] => 79755
                    )

                [TOTAL_RI] => 1000000
                [TOTAL_ANGGARAN] => 500000
            )

    )

[4566] => Array
    (
        [488] => Array
            (
                [0] => Array
                    (
                        [ID_RI_BELANJA] => 231610
                    )

                [1] => Array
                    (
                        [ID_RI_BELANJA] => 231610
                    )

                [TOTAL_RI] => 2000000
                [TOTAL_ANGGARAN] => 1000000
            )

    )
)

This is my first project and I don't know what to do.

Can anyone tell me how to do that?

Pls

4 Answers4

0

If your arrays have same Key then:

 $array1 = array(); //put your value in this array 
 $array2 = array(); //put your value in this array
 $array3 = array();
 $array3[] = $array1;
 $array3[] = $array2;
er.irfankhan11
  • 1,280
  • 2
  • 16
  • 29
0

Assuming your two arrays are $array1 and $array 2 respectively, try this:

foreach($array1 as $k1 => $v1) {
    foreach($v1 as $k2 => $v2) {
       foreach($v2 as $k3 => $v3) {
          $new[$k1][$k2][$k3] = $array1[$k1][$k2][$k3];
          $new[$k1][$k2] = array_merge($new[$k1][$k2], $array2[$k1][$k2]);
       }
   }  
}
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
0

=> store the all value in this variable

 $Arr1 //put your value in this array 
 $Arr2 //put your value in this array 

=> And merge it

$ResponseDetails    = array_merge( (array)$Arr1, (array)$Arr2);
Nimesh Patel
  • 796
  • 1
  • 7
  • 23
0

array_replace_recursive should do the job:

// $arr1 is the 1st array, $arr2 - is your 2nd array
$result = array_replace_recursive($arr1, $arr2);  // now the $result variable contains the expected merged result

http://php.net/manual/en/function.array-replace-recursive.php

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105