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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:58:00+00:00 2026-05-27T07:58:00+00:00

I am trying to use array_combine to combine two multi-dimensional arrays, but somehow not

  • 0

I am trying to use array_combine to combine two multi-dimensional arrays, but somehow not doing it correctly.

Here is array1:

Array(
    [Nov 18, 2011] => Array(
        [C] => 107705.5792
        [I] => 44561.52
    )
    [Nov 22, 2011] => Array(
        [C] => -8992.8352
    )
)

and here is array2:

Array(
    [Nov 18, 2011] => Array(
        [C] => 3
        [I] => 1
    )
    [Nov 22, 2011] => Array(
        [C] => 2
    )
)

Here is my attempt at array_combine, which is not working:

$array1 = ($arr1);
$array2 = ($arr2);
$result = array_combine($arr1, $arr2);
echo '<pre>';
print_r($result);
echo '</pre>';

What am I doing wrong? This is the result that I am looking for:

Array(
    [Nov 18, 2011] => Array(
        [3] => 107705.5792
        [1] => 44561.52
    )
    [Nov 22, 2011] => Array(
        [2] => -8992.8352
    )
)

Thanks for your help.

  • EDIT –

I have found that if I instead use array_merge_recursive, this is my the result that I get. Not what I was looking for, but close:

Array(
    [Nov 18, 2011] => Array(
        [C] => Array(
            [0] => 3
            [1] => 107705.5792
        )
        [I] => Array(
            [0] => 1
            [1] => 44561.52
        )
    )
    [Nov 22, 2011] => Array(
        [C] => Array(
            [0] => 2
            [1] => -8992.8352
        )
    )
)
  • FURTHER EDIT –

Here is the way that I have tried to implement one of the suggestions below, however this is not working for me. What is wrong?:

function cust_array_merge(array &$array1, array $array2){
    // loop through main array
    foreach ($array1 as $key => $val) {
        // check if $array2 has the same index
        if (array_key_exists($key, $array2)) {
            // reset $array1's indexes to $array2's values
            foreach ($array2[$key] as $subKey => $subVal) {
                if (array_key_exists($subKey, $array1[$key])) {
                    $tempVal = $array1[$key][$subKey];
                    unset($array1[$key][$subKey]);
                    $array1[$key][$subVal] = $tempVal;}}}}}

$merged = cust_array_merge($arr_cats_per_bill_date, $arr_cvat);
echo '<pre>';
    print_r($merged);
echo '</pre>';
  • 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-27T07:58:00+00:00Added an answer on May 27, 2026 at 7:58 am

    array_merge_recursive gets you very close (your “key” is in index 1 of leaf arrays, and your value is in index 0). So do it in two steps: first get the merged array, then fiddle with the branches to get it right.

    // This is a callback for array_map() which should have some more generic uses.
    // array(array('k', 'v'), ...) -> array('k' => 'v', ...)
    function flatten_branches($branches) {
        $newleaves = array();
        foreach ($branches as $leaf) {
            $newleaves[$leaf[0]] = $leaf[1];
        }
        return $newleaves;
    }
    
    function merge_flatten_branches($karray, $varray) {
        //$karray has the key-leaves, and $varray has the value-leaves
        $m1 = array_merge_recursive($karray, $varray);
        return array_map('flatten_branches', $m1);
    }
    
    $merged = merge_flatten_branches($array2, $array1);
    print_r($merged);
    

    Just for fun, here are two more approaches. Both of these are a bit slower than merge_flatten_branches, but illustrate some useful array concepts. (In other more functional-flavored languages than php, these might be preferred.)

    function merge_flatten_branches_reduce($karray, $varray) {
        //$karray has the key-leaves, and $varray has the value-leaves
        $m1 = array_merge_recursive($karray, $varray);
        return array_map('flatten_branches_reduce', $m1);
    }
    
    function merge_flatten_branches_add($karray, $varray) {
        //$karray has the key-leaves, and $varray has the value-leaves
        $m1 = array_merge_recursive($karray, $varray);
        return array_map('flatten_branches_add', $m1);
    }
    
    // The functions below are callbacks for the two above.
    
    function array_add($a1, $a2) {
        return $a1+$a2;
    }
    
    function flatten_leaf($leaf) {
        return array($leaf[0] => $leaf[1]);
    }
    
    function flatten_branches_add($branches) {
        $leaves = array_map('flatten_leaf', ($branches));
        $finalleaves = array();
        foreach ($leaves as $leaf) {
            $finalleaves += $leaf;
        }
        return $finalleaves;
    }
    
    
    function flatten_branches_reduce($branches) {
        $l = array_map('flatten_leaf', ($branches));
        return array_reduce($l, 'array_add', array());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use 2 dimensional array in javascript for storing strings. But
I'm trying to use jQuery's $getJSON to send an array of 'ids'. Here's what
I am trying to compare two byte arrays in memory and produce a data
I've got an array with nested arrays, and I was trying to use the
I am trying to use a for next loop to iterate through the arrays
I'm trying to use double-checked locking to maintain an array of binomial coefficients, but
I'm trying to use an array to set the where parameters for a Zend
I'm trying to use empty() in array mapping in php. I'm getting errors that
I am trying to use Jquery to iterate through an array of textboxes where
I'm trying to use a foreach loop for an array of objects. Inside of

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.