0

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}
5
  • 1
    What do you expect to obtain ? Commented Feb 4, 2019 at 11:04
  • if i print test i want like this only {"name":1,"total":"1","custom":1} one time Commented Feb 4, 2019 at 11:05
  • So, basically you want to print $data1. Also, these are not arrays - they are JSON objects. You have to json_decode() them before you can use them in PHP. See 3v4l.org/RDre9 Commented Feb 4, 2019 at 11:10
  • I'm sure its a typo when posting into the question, but {"name":1,total":"1"} is also missing a quote. Commented Feb 4, 2019 at 11:11
  • check my updated question Commented Feb 4, 2019 at 11:19

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

Used array_merge() and then perform array_unique()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.