合并数组的一部分递归 php

merge a part of array recursive php

我尝试用谷歌搜索但找不到任何接近的东西。 是否可以合并两个数组但实际上是第二个数组的一部分?

这些数组会很长,第二个我只想使用第二个数组中的类别并首先合并!

我的数组:

$ar1 = array(
    "locale" => "en-US",
    "id" => 1,
    "categories" => array(
        "0" => array("name" => "abc", "username" => "abc1"),
        "1" => array("name" => "cdf", "username" => "bbb3"),
    )
);

$ar2 = array(
    "locale" => "en-US",
    "id" => 1,
    "categories" => array(
        "0" => array("name" => "xyz", "username" => "xyz4"),
        "1" => array("name" => "zyx", "username" => "xtt44"),
    )
);

我尝试了array_mergearray_merge_recursive但是它不起作用。

我得到

Array
(
    [locale] => Array
        (
            [0] => en-US
            [1] => en-US
        )

    [id] => Array
        (
            [0] => 1
            [1] => 1
        )

    [categories] => Array
        (
            [0] => Array
                (
                    [name] => abc
                    [username] => abc1
                )

            [1] => Array
                (
                    [name] => cdf
                    [username] => bbb3
                )

            [2] => Array
                (
                    [name] => xyz
                    [username] => xyz4
                )

            [3] => Array
                (
                    [name] => zyx
                    [username] => xtt44
                )

        )

)

但这就是我想要的:

Array
(
    [locale] => Array
        (
            [0] => en-US
        )

    [id] => Array
        (
            [0] => 1
        )

    [categories] => Array
        (
            [0] => Array
                (
                    [name] => abc
                    [username] => abc1
                )

            [1] => Array
                (
                    [name] => cdf
                    [username] => bbb3
                )

            [2] => Array
                (
                    [name] => xyz
                    [username] => xyz4
                )

            [3] => Array
                (
                    [name] => zyx
                    [username] => xtt44
                )

        )

)

您可能想要使用 array_replace or array_replace_recursive。 或者我最近写的这个函数(基于网上找到的一些代码):https://gist.github.com/jehaby/e91b6d35661ba8900644e5d64cc0055b

array_combine 或像这样使用 array_merge:

array_merge($ar1, $ar2['categories']);

这里需要使用foreach循环。因为你们的索引是相似的。

$result = array();

$result['locale'] = array('en-US');
$result['id'] = array(1);
foreach($ar1['categories'] as $val){
    $result['categories'][] = array('name' => $val['name'], 'username' => $val['username']);
}

foreach($ar2['categories'] as $val){
    $result['categories'][] = array('name' => $val['name'], 'username' => $val['username']);
}

echo '<pre>';
print_r($result);

结果

Array
(
    [locale] => Array
    (
        [0] => en-US
    )

    [id] => Array
    (
        [0] => 1
    )
    [categories] => Array
        (
            [0] => Array
                (
                    [name] => abc
                    [username] => abc1
                )

            [1] => Array
                (
                    [name] => cdf
                    [username] => bbb3
                )

            [2] => Array
                (
                    [name] => xyz
                    [username] => xyz4
                )

            [3] => Array
                (
                    [name] => zyx
                    [username] => xtt44
                )

        )

)

这可能不是内置函数。是的;它只是一个带有分支条件的嵌套循环。 测试它...看看(最重要的)它是否能满足您的需求...剩下的就是选择问题:简单胜过复杂。

这是给定的数组:$ar1$ar2

    <?php

        $ar1 = array(
            "locale" => "en-US",
            "id" => 1,
            "categories" => array(
                "0" => array("name" => "abc", "username" => "abc1"),
                "1" => array("name" => "cdf", "username" => "bbb3"),
            )
        );

        $ar2 = array(
            "locale" => "en-US",
            "id" => 1,
            "categories" => array(
                "0" => array("name" => "xyz", "username" => "xyz4"),
                "1" => array("name" => "zyx", "username" => "xtt44"),
            )
        );

这是循环结构:

    <?php

        $arrResult  = array();

        foreach($ar1 as $key=>$value){
            if(!array_key_exists($key, $arrResult)){

                if(is_array($value)){
                    $arrResult[$key] = array();
                    for($i=0; $i<count($value); $i++){
                        $arrResult[$key][] = $value[$i];
                    }
                }else{
                    $arrResult[$key] = $value;
                }

                foreach ($ar2 as $index => $item) {
                    if(!array_key_exists($index, $arrResult)){
                        if(is_array($item)){
                            if($key == $index) {
                                $arrResult[$index] = array();
                                for ($j = 0; $j < count($item); $j++) {
                                    $arrResult[$key][] = $item[$j];
                                }
                            }
                        }
                    }else{
                        if(is_array($item)){
                            if($key == $index) {
                                for ($j = 0; $j < count($item); $j++) {
                                    array_push($arrResult[$index], $item[$j]);
                                }
                            }
                        }else{
                            $arrResult[$index] = $item;
                        }
                    }
                }

            }
        }

        var_dump($arrResult);

?>

这是 var_dump() 的输出:

    array (size=3)
        'locale' => string 'en-US' (length=5)
        'id' => int 1
    'categories' =>
    array (size=4)
        0 =>
        array (size=2)
            'name' => string 'abc' (length=3)
            'username' => string 'abc1' (length=4)
        1 =>
        array (size=2)
            'name' => string 'cdf' (length=3)
            'username' => string 'bbb3' (length=4)
        2 =>
        array (size=2)
            'name' => string 'xyz' (length=3)
            'username' => string 'xyz4' (length=4)
        3 =>
        array (size=2)
            'name' => string 'zyx' (length=3)
            'username' => string 'xtt44' (length=5)

我仍然相信您可能需要根据您的用例和偏好添加自己的逻辑(如果您喜欢的话)。