1

I have arrays and these arrays have same values and I want to remove the same values and print the different values.

Arrays:

Array(
    [0] => Likes
    [7] => Frind
    [8] => USA
)
Array(
    [0] => USA
    [5] => Lools
)
Array(
    [6] => USA
    [12] => Awesome
)

I need to like this:

Array(
    [0] => Likes
    [7] => Frind
    [8] => USA
    [5] => Lools
    [12] => Awesome
)
2
  • do the keys of the arrays matter? Commented Jan 28, 2018 at 15:24
  • @murtho no don't matter Commented Jan 28, 2018 at 15:25

2 Answers 2

3

First you should merge the arrays:

$merged = array_merge($array1, $array2, $array3);

Then you can use array_unique method

$unique_array = array_unique($merged);
Sign up to request clarification or add additional context in comments.

1 Comment

You can pass every arrays to the array_merge method.
2

Try this

$array1 = ['likes', 'friends', 'USA']
$array2 = ['USA', 'lools']
$array3 = ['USA', 'Awesome']

$temp_array = array_unique(array_merge($array1, $array2));
$final_array = array_unique(array_merge($temp_array, $array2));

9 Comments

this arrays automatically created my friend and cant to save this to separate arrays ...!
If it so pass them directly in array merge parameter there is no need to create an additional variable $temp_arrray
Can you give me example please !
How are you generating the arrays?
do it like this. make a $final_array = [] and keep on updating in the foreach loop by $final_array = array_unique(array_merge($final_array, $finalarrays))
|

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.