php array_merge_recursive保留数字键 [英] php array_merge_recursive preserving numeric keys

查看:154
本文介绍了php array_merge_recursive保留数字键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想合并

$a = array("59745506"=>array("up" => 0,));
$b = array("59745506"=>array("text" => "jfrj"));
$c = array_merge_recursive_new($a, $b);

结果:

Array
(
    [0] => Array
        (
            [up] => 0
        )

    [1] => Array
        (
            [text] => jfrj
        )

)

预期结果:

    Array
(
    [59745506] => Array
        (
            [up] => 0
            [text] => jfrj
        )

)

http://www.php中的第二条注释. net/manual/en/function.array-merge-recursive.php 是否有效,这是否是我的最佳解决方案(我需要合并具有多个数字键和2个级别的数组)?

另一个解决方法是使用array_map(function()...

来实现它

解决方案

array_replace_recursive() 函数看起来像是您需要什么.

$a = array("59745506" => array("up" => 0,));
$b = array("59745506" => array("text" => "jfrj"));
$c = array_replace_recursive($a, $b);
var_export($c);

// array (
//   59745506 => 
//   array (
//     'up' => 0,
//     'text' => 'jfrj',
//   ),
// )

I would simply like to merge

$a = array("59745506"=>array("up" => 0,));
$b = array("59745506"=>array("text" => "jfrj"));
$c = array_merge_recursive_new($a, $b);

result:

Array
(
    [0] => Array
        (
            [up] => 0
        )

    [1] => Array
        (
            [text] => jfrj
        )

)

expected result:

    Array
(
    [59745506] => Array
        (
            [up] => 0
            [text] => jfrj
        )

)

the 2nd comment in http://www.php.net/manual/en/function.array-merge-recursive.php is working, is it the best solution for my case (where I need to merge arrays with multiple numeric keys, and with 2 levels)?

another workaround would be to implement it with array_map(function ()...

解决方案

The array_replace_recursive() function looks to be what you need.

$a = array("59745506" => array("up" => 0,));
$b = array("59745506" => array("text" => "jfrj"));
$c = array_replace_recursive($a, $b);
var_export($c);

// array (
//   59745506 => 
//   array (
//     'up' => 0,
//     'text' => 'jfrj',
//   ),
// )

这篇关于php array_merge_recursive保留数字键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆