PHP数组合并重复发生

问题描述 投票:0回答:2

我有一个2点阵列命名的数据和数据1都是相同的一组键和值,但数据1有一些自定义键和值,所以我想在这个合并

data array

[0] => Array
    (
        [name] => 1
        [total] => 1
    )

[1] => Array
    (
        [name] => 2
        [total] => 1
    )

[2] => Array
    (
        [name] => 3
        [total] => 3
    )

data1 array

[0] => Array
    (
        [name] => 1
        [total] => 1
        [custom] => 1
    )

[1] => Array
    (
        [name] => 2
        [total] => 1
        [custom] => 1
    )

[2] => Array
    (
        [name] => 3
        [total] => 3
        [custom] => 1
    )

$test = array_merge(data,data1);

所以我用array_merge(数据,DATA1)它显示重复的

合并后,我得到了这样的

print_r(test);
{"name":1,total":"1"}
{"name":1,"total":"1","custom":1}
php arrays
2个回答
1
投票
array_unique(array_merge($array1,$array2), SORT_REGULAR);

http://se2.php.net/manual/en/function.array-unique.php


0
投票

二手array_merge(),然后执行array_unique()

© www.soinside.com 2019 - 2024. All rights reserved.