0

I am trying to add 1st array value to 2nd array based on key match but not success with array_merge function see example of what i doing below

Array1
(
    [99] => 99
    [98] => 98
    
)
Array2


(
    [99] => Array
        (
            [0] => 1300
            [1] => 1500
            [2] => 1618
            [3] => 2704
            [4] => 1401
            [5] => 1900
            [6] => 1100
            [7] => 4232
            [8] => 4233
        )

    [98] => Array
        (
            [0] => 1400
            [1] => 4802
            [2] => 1601
            [3] => 1603
            [4] => 1100
            [5] => 1900
        )
    )

my code so far returning me wrong output like this

$finalArray = array_merge($Array1, $Array2);

what i am expecting

finalArray
(
    
   [99] => Array
        (
            [0] => 1300
            [1] => 1500
            [2] => 1618
            [3] => 2704
            [4] => 1401
            [5] => 1900
            [6] => 1100
            [7] => 4232
            [8] => 4233
            [9] => 99
        )

    [98] => Array
        (
            [0] => 1400
            [1] => 4802
            [2] => 1601
            [3] => 1603
            [4] => 1100
            [5] => 1900
            [6] => 98
        )
    )

i have tried array_merge, array_push & array combine as well but not success please somebody help me on this

uzthegeek
  • 43
  • 6
  • 1
    Does this answer your question? [PHP Array Merge two Arrays on same key](https://stackoverflow.com/questions/14842272/php-array-merge-two-arrays-on-same-key) – nice_dev Aug 30 '22 at 06:17
  • @uzthegeek There are tons of duplicates already for this. Did you follow those approaches? – nice_dev Aug 30 '22 at 06:24
  • @nice_dev i have tried they are single array my array is multidimensional that's why failed – uzthegeek Aug 30 '22 at 06:26
  • 1
    Just loop over your first array, and then add the value under the same key in the second ... `foreach($array1 as $key => $value) { $array2[$key][] = $value; }` – CBroe Aug 30 '22 at 06:34
  • @CBroe i did but not working ;( – uzthegeek Aug 30 '22 at 06:37
  • 1
    Show a proper example of what exactly you tried then please. (Edit the question.) – CBroe Aug 30 '22 at 06:38
  • @CBroe i have showed proper example in simple words i want to add array1 value into array2 values based on both array key match that is simple !!! – uzthegeek Aug 30 '22 at 06:43
  • _"i have showed proper example"_ - not of your attempt to implement my suggestion. How are we supposed to tell you what you did _wrong_, when you don't show us what exactly you did? I can only assume that you simply did not pay enough attention to make the variables names match yours, because other than that, my suggestion works the same as the answer you now accepted. https://3v4l.org/CjkTd – CBroe Aug 30 '22 at 07:20

1 Answers1

1

This should do the work for you

Example code

   <?php
    $arr1 = Array
    (
        '99' => 99,
        '98' => 98
        
    );

    $arr2 = Array(
        '99' => Array
            (
                '0' => 1300,
                '1' => 1500,
                '2' => 1618,
                '3' => 2704,
                '4' => 1401,
                '5' => 1900,
                '6' => 1100,
                '7' => 4232,
                '8' => 4233
            ),
        '98' => Array
            (
                '0' => 1400,
                '1' => 4802,
                '2' => 1601,
                '3' => 1603,
                '4' => 1100,
                '5' => 1900
            )
        );
        
     foreach($arr1 as $key => $value){
      if(isset($arr2[$key])){
        array_push($arr2[$key],$value);
      }else{
        $arr2[$key] = $value;
      }
    }
        
        print_r($arr2);
bobi
  • 169
  • 2
  • 16