"array_unique vs array_flip" Answer’s


I benchmarked it for you: CodePad

Your intuition on this was correct!

for($run=0; $run<1000; $run++)


for($run=0; $run<100; $run++)

echo 'Array Unique: '.$time."n";


for($run=0; $run<100; $run++)

echo 'Keys Flip: '.$time."n";


for($run=0; $run<100; $run++)

echo 'Flip Flip: '.$time."n";


Array Unique: 1.1829199790955
Keys Flip: 0.0084578990936279
Flip Flip: 0.0083951950073242

Note that array_keys(array_flip($array)) will give a new key values in order, which in many cases may be what you want (identical except much faster to array_values(array_unique($array))), whereas array_flip(array_flip($array)) is identical (except much faster) to array_unique($array) where the keys remain the same.

Wednesday, March 31, 2021
answered 10 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :