PHP数组合并重复发生

时间:2019-02-04 11:03:13

标签: php arrays

我有一个名为data&data1的2数组,它们都是相同的键和值集,但是data1有一些自定义键和值 所以我想在这里合并

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(data,data1)它显示重复

合并后,我就这样

print_r(test);
{"name":1,total":"1"}
{"name":1,"total":"1","custom":1}

2 个答案:

答案 0 :(得分:1)

array_unique(array_merge($array1,$array2), SORT_REGULAR);

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

答案 1 :(得分:0)

使用array_merge()然后执行array_unique()