合并一部分数组递归php

时间:2016-05-11 15:51:05

标签: php arrays array-merge

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

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

我的阵列:

$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_merge array_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
                )

        )

)

4 个答案:

答案 0 :(得分:0)

您可能希望使用array_replacearray_replace_recursive。 或者我最近写的这个函数(基于互联网上的一些代码):https://gist.github.com/jehaby/e91b6d35661ba8900644e5d64cc0055b

答案 1 :(得分:0)

array_combine或使用array_merge,如下所示:

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

答案 2 :(得分:0)

你需要在这里使用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
                )

        )

)

答案 3 :(得分:0)

这可能不是内置功能。是;它只是一个带有分支条件的嵌套循环。 测试它......看看(最重要的)它是否符合你的要求......剩下的就是选择: 简单而不是复杂。

以下是给定阵列: $ 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)

我仍然相信您可能需要添加自己的逻辑(如果您喜欢),具体取决于您的用例和偏好。