PHP combines two multidimensional arrays

I am trying to use array_combine to combine two multidimensional arrays, but somehow not doing it right.

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 that does not work:

$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 I'm looking for:

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

Thank you for your help.

  • EDIT -

I found that if I use array_merge_recursive instead, this is my 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 EDITION -

This is how I tried to implement one of the suggestions below, however this does not work for me. What's 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 indexes to $array2 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>';
+5
source share
2 answers

array_merge_recursive ( "" 1 , 0). , : , , .

// 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);

, . , merge_flatten_branches, . ( , php, .)

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());
}
+3

, , . , PHP, . . : , , .

function cust_array_merge(array $array1, array $array2)
{
    $merged = array();
    // loop through main array
    foreach ($array1 as $key => $val) {
        // check if $array2 has the same index
        if (array_key_exists($key, $array2)) {
            // reset $array1 indexes to $array2 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 = $array1;
        }
    }
    return $merged;
}
+1

All Articles