I think Dan Fego gave a wonderful answered as to why one would sort an array prior to removing duplicates; however, I’d like to examine what array_flip()
does. I’ll be using the following array to illustrate:
'a' => 'apple'
'b' => 'banana'
'c' => 'apple'
'd' => 'date'
array_flip()
exhanges the keys and values producing
'apple' => 'a'
'banana' => 'b'
'apple' => 'c'
'date' => 'd'
However, keys must be unique. The manual describes how array_flip()
handles this:
If a value has several occurrences, the latest key will be used as its
values, and all others will be lost.
So we get something like this:
'banana' => 'b'
'apple' => 'c'
'date' => 'd'
So if we use array_flip(array_flip())
we get:
'b' => 'banana'
'c' => 'apple'
'd' => 'date'
As for the motivation behind array_unique()
, we can only speculate unless Rasmus Lerdorf or someone currently working on PHP development cares to answer.