Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 987
Lachlan McDonald Avatar asked Feb 06 '10 13:02

Lachlan McDonald


2 Answers

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
//         )
// 
// )
like image 70
artlung Avatar answered Oct 23 '22 08:10

artlung


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)     
like image 21
soulmerge Avatar answered Oct 23 '22 10:10

soulmerge