Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all instances of duplicated values from an array

Tags:

arrays

php

I know there is array_unique function, but I want to remove duplicates. Is there a built-in function or do I have to roll my own.

Example input:

banna, banna, mango, mango, apple

Expected output:

apple
like image 334
Aivan Monceller Avatar asked Oct 14 '10 13:10

Aivan Monceller


1 Answers

You can use a combination of array_unique, array_diff_assoc and array_diff:

array_diff($arr, array_diff_assoc($arr, array_unique($arr)))
like image 155
Gumbo Avatar answered Nov 01 '22 12:11

Gumbo