Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:40:02+00:00 2026-06-08T22:40:02+00:00

How to merge and overwrite multidimensional array with same key and value with array_merge_recursive

  • 0

How to merge and overwrite multidimensional array with same key and value with array_merge_recursive ? Supposed I Have two array like below:

$arr1 = array(       
   // OVERWRITE
   array('prop_id' => 1, 'prop_value' => 'batman'),
   array('prop_id' => 2, 'prop_value' => 'ironman'),
   // NOT OVERWRITE
   array('prop_id' => 5, 'prop_value' => 'wonderwoman'),
);

$arr2 = array(
   array('prop_id' => 1, 'prop_value' => 'robin'),
   array('prop_id' => 2, 'prop_value' => 'superman'),
   array('prop_id' => 4, 'prop_value' => 'catwoman'),
);

I want to merge and overwrite it with new value (the rule is comparison key with the same value it not overwrite), the expected result is

 $result = array_merge_overwrite($arr1, $arr2, array('prop_id') /* Comparison Key */);
 $result = array(       
   array('prop_id' => 1 /* Comparison Key */, 'prop_value' => 'robin' /* Comparison value */),
   array('prop_id' => 2, 'prop_value' => 'superman'),
   array('prop_id' => 4, 'prop_value' => 'catwoman'),
   array('prop_id' => 5, 'prop_value' => 'wonderwoman'),
);

With array_merge_recursive it appended not overwrited, I try with array_replace_recursive like below:

$result = array_replace_recursive(
    array(
       1 => array('prop_id' => 1, 'prop_value' => 'batman'),
       2 => array('prop_id' => 2, 'prop_value' => 'ironman'),
       5 => array('prop_id' => 5, 'prop_value' => 'wonderwoman'),
    ),
    array(
       1 => array('prop_id' => 1, 'prop_value' => 'robin'),
       2 => array('prop_id' => 2, 'prop_value' => 'superman'),
       4 => array('prop_id' => 4, 'prop_value' => 'catwoman'),
    ),
);

It works, but my code look nasty and dirty. Any better solution than mine

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:40:03+00:00Added an answer on June 8, 2026 at 10:40 pm

    Here is a function that would work as you described:

    function array_merge_overwrite($arr1, $arr2, $uniques=array('prop_id'), $delimiter='-')
    {
        $result = array();
        $uk = array();
        foreach($arr1 as $a1)
        {   
            $uk = array();
            foreach($uniques as $u) $uk[] = $a1[$u];
            $result[implode($delimiter, $uk)] = $a1;
        }   
    
        foreach($arr2 as $a2)
        {   
            $uk = array();
            foreach($uniques as $u) $uk[] = $a2[$u];
            $result[implode($delimiter, $uk)] = $a2;
        }   
        return $result;
    }
    

    If passed $arr1 and $arr2 as defined in the question, the above function will return an array:

    Array
    (
        [1] => Array
            (
                [prop_id] => 1
                [prop_value] => robin
            )
    
        [2] => Array
            (
                [prop_id] => 2
                [prop_value] => superman
            )
    
        [5] => Array
            (
                [prop_id] => 5
                [prop_value] => wonderwoman
            )
    
        [4] => Array
            (
                [prop_id] => 4
                [prop_value] => catwoman
            )
    
    )
    

    Of course, if you were to always and only use prop_id as the unique element then the function could be quite a bit simpler:

    function array_merge_overwrite($arr1, $arr2)
    {
        $tmp = array();
        foreach($arr1 as $a1) $tmp[$a1['prop_id']] = $a1['prop_value'];
        foreach($arr2 as $a2) $tmp[$a2['prop_id']] = $a2['prop_value'];
        $result = array();
        foreach($tmp as $k=>$v) $result[] = array('prop_id'=>$k, 'prop_value'=>$v);
        return $result;
    }
    

    The only difference in the returned array in this later function is that the keys of the element arrays will be the standard numerical values instead of matching the prop_ids

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a trunk (A) and two branches (B and C). When I merge
I have two javascript object with data like this: { data: [ { foo:
I merge two facebook-requests, by pushing them both into a new object. That's why
Each time I use Merge() I have the following: 'Cannot implicitly convert type 'void'
I want to merge all data in two arraylist in c#. Some data in
I have solved some merge conflicts, committed then tried to Push my changes and
I want to merge two arrays and replace the text with strtr function. I
I am looking to have multiple users edit the same image using the gd
I have a repository with two branches: live and stage. The repository holds code
I need to merge some arrays in some different way and I use array_merge_recursive.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.