Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

arrays - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...