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

  • Home
  • SEARCH
  • 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 582513
In Process

The Archive Base Latest Questions

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

I have a query that I run several times with different parameters. I am

  • 0

I have a query that I run several times with different parameters. I am using an xml parser to return the results to jQuery.

I can’t seem to find a way to combine the results on the first ‘node’ without overwriting the first entry.

Simplified code sample:

$temp1 = returnArray(0);

$temp2 = returnArray(1);

$data = array_merge($temp1, $temp2);

print_r($data);

function returnArray($switch)
{
    if($switch == 0)
    {
        $data['data']['first'] = "something";
    } else {
        $data['data']['second'] = "something ELSE";
    }
    return $data;
}

results in printing out Array ( [data] => Array ( [second] => something ELSE ) ) – $temp2 is overwriting $temp1. I understand this to be the default behavior of array_merge(), which is confusing to me.

I have also tried doing something like $data = $temp1+$temp2; or…

$data = returnArray(0);

$data += returnArray(1);

prints Array ( [data] => Array ( [first] => something ) )

or…

$data = returnArray(0);

$data .= returnArray(1);
print_r($data);

prints ArrayArray

I ended up hacking this together:

$data['data'] = returnArray(0);
$data['data'] = returnArray(1);

function returnArray($switch){
  if($switch == 0) return Array('first' => 'something');
  else return Array('second' => 'something else');
}

Which i’m not all that happy with… Although it gets the job done in this case, it won’t be all that applicable in the future when a more complex situation occurs.

Is there a better way to take two associative, multi-dimensional arrays and combine them without overwriting?

EDIT:

I’d like the method to be reusable, extensible and be able to handle 2+ arrays with 2+ dimensions.

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

    PHP actually provides a function called array_merge_recursive() which is used exactly in this type of scenario. From the documentation page:

    array_merge_recursive() merges the
    elements of one or more arrays
    together so that the values of one are
    appended to the end of the previous
    one. It returns the resulting array.

    If the input arrays have the same
    string keys, then the values for these
    keys are merged together into an
    array, and this is done recursively,
    so that if one of the values is an
    array itself, the function will merge
    it with a corresponding entry in
    another array too. If, however, the
    arrays have the same numeric key, the
    later value will not overwrite the
    original value, but will be appended.

    Example:

    <?php
    $ar1 = array("color" => array("favorite" => "red"), 5);
    $ar2 = array(10, "color" => array("favorite" => "green", "blue"));
    $result = array_merge_recursive($ar1, $ar2);
    print_r($result);
    ?>
    

    Output:

    Array
    (
        [color] => Array
            (
                [favorite] => Array
                    (
                        [0] => red
                        [1] => green
                    )
    
                [0] => blue
            )
    
        [0] => 5
        [1] => 10
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using in C# MYsql .I have query that works if I run
I have a query that is currently using a correlated subquery to return the
I have an application that may be run several times a day. Each run
I have the next query that I run via SQLCMD.EXE use [AxDWH_Central_Reporting] GO EXEC
I have a java.sql.ResultSet object containg data from a query that was run. How
I have a SQL query that takes a very long time to run on
I have an update query being run by a cron task that's timing out.
I run a query, where I count several sum s on the different fields
I have the following SQL query that I am trying to run inside Microsoft
I have a query that is dynamically built after looking up a field list

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.