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 6233417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:17:47+00:00 2026-05-24T10:17:47+00:00

I need to merge some arrays in some different way and I use array_merge_recursive.

  • 0

I need to merge some arrays in some different way and I use array_merge_recursive.
However there is something that I need to change and I don’t know how.
Here is quote from php.net

If, however, the arrays have the same numeric key, the later value
will not overwrite the original value, but will be appended.

I want this value, NOT to be appended, I want not to append exact values in the new array.Hope you’ve understood this.

Example:

$array = array(
   'some'  => array(
       'other'    => 'key',
   ),
);

$array2 = array();
$array2['some']['other'] = 'key2';

If I use array_merge_recursive It will result this:

Array (
    [some] => Array
        (
            [other] => Array
                (
                    [0] => key
                    [1] => key2
                )
        ) )

I want if it matches the same result, not to append it.Yes I know, you would say, then use array_merge, but it doesn’t work well, too.
If I use this:

$array = array(
   'some'  => array(
       'other'    => 'key',
   ),
);

$array2 = array();
$array2['some']['other2'] = 'key2';

print_r(array_merge($array, $array2));

It will remove $array[some][other] from the list and leave only $array[some][other2].I don’t know which is better, since no one makes it better.

  • 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-05-24T10:17:48+00:00Added an answer on May 24, 2026 at 10:17 am

    try this

    <?php
    function mymerge(&$a,$b){ //$a will be result. $a will be edited. It's to avoid a lot of copying in recursion
        foreach($b as $child=>$value){
            if(isset($a[$child])){ 
                if(is_array($a[$child]) && is_array($value)){ //merge if they are both arrays
                    mymerge($a[$child],$value);
                }
                //else ignore, you can add your own logic, i.e when 1 of them is array
            }
            else
                $a[$child]=$value; //add if not exists
        }
    
        //return $a;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays containing different keys and values. However, some keys store strings
I need to merge two SELECT statements. There are two tables in my database,
So I think I am almost there conceptually but need some missing pointers. Objective
I have two arrays that need to be merged together and trying to figure
I need some help with a program that I am writing for my Systems
I need to merge multiple arrays together, but I only want to KEEP the
I have some xml files that will be read-only that I need to access
My application will need some windows but I can't use the popUpManager to handle
I need to merge two XML documents, overwriting the overlapsed attributes and elements. For
I need to merge a whole bunch of docx files programatically. Imagine they are

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.