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
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
The answer on top looks great, but on a side note: if you ever want to eliminate duplicates but leave the first one, using array_flip twice would be a pretty simple way to do so. array_flip(array_flip(x))
You can use
See
array_count_values
— Counts all the values of an arrayarray_filter
— Filters elements of an array using a callback functionarray_keys
— Return all the keys or a subset of the keys of an arrayYou want to remove any entries that have duplicates, so that you're left with only the entries that were unique in the list? Hmm it does sound like something you'll need to roll your own.
There is no existing function; You'll have to do this in two passes, one to count the unique values and one to extract the unique values:
You can use a combination of
array_unique
,array_diff_assoc
andarray_diff
:Just write your own simple foreach loop:
have fun :)