my Test
my Test

Reputation: 105

php array merge duplicate occure

I have a 2 array named data & data1 both are same set of key and value but data1 have some custom key & value so i want to merge in this

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);

so i used array_merge(data,data1) it shows duplicate

after merge i got like this

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

Upvotes: 0

Views: 55

Answers (2)

Sagar Pabale
Sagar Pabale

Reputation: 1

Used array_merge() and then perform array_unique()

Upvotes: 0

MD. Jubair Mizan
MD. Jubair Mizan

Reputation: 1570

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

https://www.php.net/manual/en/function.array-unique.php

Upvotes: 1

Related Questions