Lachlan McDonald
Lachlan McDonald

Reputation: 2434

PHP's array_merge_recursive behaviour on integer keys

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?

Upvotes: 10

Views: 3462

Answers (5)

mickmackusa
mickmackusa

Reputation: 47991

array_merge_recursive() simply isn't the right tool for merging two 2d array with numeric first level keys.

Use a standard loop to merge the second array's values into the first array on the related first level key.

Code: (Demo)

foreach ($b as $k => $v) {
    $a[$k] = array_merge($a[$k] ?? [], $v);
}
var_export($a);

Upvotes: 0

Nikolay Zubkov
Nikolay Zubkov

Reputation: 36

This recursive array merge function doesn't renumber integer keys and appends new values to existing ones OR adds a new [key => value] pair if the pair doesn't exist. I suppose and exen sure, that this function is that what you need.

function array_merge_recursive_adv(array &$array1, $array2) {
        if(!empty($array2) && is_array($array2))
            foreach ($array2 as $key => $value) {
                    if(array_key_exists($key,$array1)) {
                        if(is_array($value)){
                            array_merge_recursive_adv($array1[$key], $value);
                        } else {
                             if(!empty($array1[$key])) {
                                if(is_array($array1[$key])){
                                    array_push($array1[$key], $value);
                                } else {
                                    $array1[$key] = [$array1[$key]];
                                    $array1[$key][] = $value;
                                }
                             } else if(empty($array1[$key])) {
                                $array1[$key] = $value;
                             }
                        }
                    } else {
                        $array1[$key] = $value;
                    }
            }
            return $array1;
    }

Upvotes: 0

soulmerge
soulmerge

Reputation: 75724

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)     

Upvotes: 2

useless
useless

Reputation: 1906

If you want the keys to be treated as strings just make it strings adding a prefix when you fill it, instead of to fill it with numbers and then refill another array just to order it.

Upvotes: 0

artlung
artlung

Reputation: 33833

I'm using soulmerge's idea of converting the keys by adding a string. My new function can only handle 2 parameters, however, but that was the case you had, so that's what I went with. Have a look.

// Adds a _ to top level keys of an array
function prefixer($array) {
    $out = array();
    foreach($array as $k => $v) {
        $out['_' . $k] = $v;
    }
    return $out;
}
// Remove first character from all keys of an array
function unprefixer($array) {
    $out = array();
    foreach($array as $k => $v) {
        $newkey = substr($k,1);
        $out[$newkey] = $v;
    }
    return $out;
}
// Combine 2 arrays and preserve the keys
function array_merge_recursive_improved($a, $b) {
    $a = prefixer($a);
    $b = prefixer($b);
    $out = unprefixer(array_merge_recursive($a, $b));
    return $out;
}

And what's out sample data look like?

// some sample data    
$a = array(
    '123' => array(1)
);
$b = array(
    '123' => array(2, 3)
);

// what do the results say:    
print_r($a);
// Array
// (
//     [123] => Array
//         (
//             [0] => 1
//         )
// 
// )

print_r($b);
// Array
// (
//     [123] => Array
//         (
//             [0] => 2
//             [1] => 3
//         )
// 
// )

And let's try them out:

print_r(array_merge_recursive($a, $b));
// Array
// (
//     [0] => Array
//         (
//             [0] => 1
//         )
// 
//     [1] => Array
//         (
//             [0] => 2
//             [1] => 3
//         )
// 
// )

print_r(array_merge_recursive_improved($a, $b));
// Array
// (
//     [123] => Array
//         (
//             [0] => 1
//             [1] => 2
//             [2] => 3
//         )
// 
// )

Upvotes: 3

Related Questions