具有自定义函数的array_replace_recursive,如Lodash mergeWith

时间:2017-11-17 11:02:35

标签: php arrays recursion

我需要一个与array_replace_recursive相同的函数,但是新创建的值保持空字符串。

JSON 1
{
    "test1":"test1",
    "test2":"test2"
}

JSON 2 before
{
    "test1":"test1"
}

JSON 2 after
{
    "test1":"test1",
    "test2" : ""
}

你有什么想法吗?

我正在使用个人递归函数进行测试,以便继续 ^^

(如https://lodash.com/docs/#mergeWith

提前谢谢。

1 个答案:

答案 0 :(得分:1)

NOTICE Undefined offset: 1 /usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/main/functions.php 11536
WARNING
WARNING
WARNING
NOTICE Undefined index: pgtimezone /usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/main/functions.php 68
NOTICE Undefined index: no_dir_exists /usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/main/backup.php 245
NOTICE Undefined offset: 1 /usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/main/functions.php 11536
WARNING
WARNING
WARNING
NOTICE Undefined index: pgtimezone /usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/main/functions.php 68
NOTICE Undefined index: no_dir_exists /usr/local/cpanel/whostmgr/docroot/cgi/softaculous/enduser/main/backup.php 245

输出:

<?php

$a = [
    'foo' =>
    [
        'bar' => 'Stool',
        'bat' => 'Cricket'
    ]
];

$b = [
    'foo' =>
    [
        'bar' => 'Tat'
    ],
    'qux' => 
    [
        'quux' => 'Cross'
    ]
];

array_walk_recursive($b,
    function(&$v) {
        $v = "";
    }
);
$c = array_replace_recursive($a, $b);

var_export($c);