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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:55:34+00:00 2026-05-13T14:55:34+00:00

Is there an approach for recursively merging arrays, in the same way as PHP’s

  • 0

Is there an approach for recursively merging arrays, in the same way as PHP’s array_merge_recursive() function does, except that integer keys are treated the same as string keys?

(It’s important for the process that the keys remain parse-able as integers.)

For example:

$a = array(
    'a' => array(1)
);
$b = array(
    'a' => array(2, 3)
);
var_dump(array_merge_recursive($a, $b));

Will merge the on the "a" key and output, as expected, the following:

array(1) {
    ["a"] => array(3) {
        [0] => int(1)
        [1] => int(2)
        [2] => int(3)
    }
}

However, when using integers for keys (even when as a string):

$a = array(
    '123' => array(1)
);
$b = array(
    '123' => array(2, 3)
);
var_dump(array_merge_recursive($a, $b));

array_merge_recursive() will return:

array(2) {
    [0] => array(3) {
        [0] => int(1)
    }
    [1] => array(2) {
        [0] => int(2)
        [1] => int(3)
    }
}

Instead of the much desired:

array(1) {
    ["123"] => array(3) {
        [0] => int(1)
        [1] => int(2)
        [2] => int(3)
    }
}

Thoughts?

  • 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-13T14:55:35+00:00Added an answer on May 13, 2026 at 2:55 pm

    you can prefix the array keys with a short string:

    function addPrefix($a) {
        return '_' . $a;
    }
    # transform keys
    $array1 = array_combine(array_map('addPrefix', array_keys($array1)), $array1);
    $array2 = array_combine(array_map('addPrefix', array_keys($array2)), $array2);
    # call array_combine
    $array = array_merge_recursive($array1, $array2);
    # reverse previous operation  
    function stripPrefix($a) {
         return substr($a, 1);
    }
    $array = array_combine(array_map('stripPrefix', array_keys($array)), $array)     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: There must be some way I can approach this without writing a whole
I need to know if there's any way (or another distinct approach) to an
I got a Function that returns a Collection<string> , and that calls itself recursively
is there an easy approach to store my model, which I use with EclipseLink,
Is there a universal approach in Python, to find out the path to the
Is there a jQuery approach to detect whether a page is loaded from the
I'm wondering if there is any approach to implement generic null object pattern in
Is there a push notification plugin/approach for phonegap (android/iphone) where: the application can still
Is there any easy library or approach to get the week (from which date
Am wondering if there would be any security flaw in this approach. I am

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.