Does array_unique() modify its array by reference?
$uniquearr = array_unique($myarr); var_dump($uniquearr); Always read docs first http://www.php.net/array_unique array_unique Return Values Returns the filtered array.
$uniquearr = array_unique($myarr); var_dump($uniquearr); Always read docs first http://www.php.net/array_unique array_unique Return Values Returns the filtered array.
I benchmarked it for you: CodePad Your intuition on this was correct! $test=array(); for($run=0; $run<1000; $run++) $test[]=rand(0,100); $time=microtime(true); for($run=0; $run<100; $run++) $out=array_unique($test); $time=microtime(true)-$time; echo ‘Array Unique: ‘.$time.”\n”; $time=microtime(true); for($run=0; $run<100; $run++) $out=array_keys(array_flip($test)); $time=microtime(true)-$time; echo ‘Keys Flip: ‘.$time.”\n”; $time=microtime(true); for($run=0; $run<100; $run++) $out=array_flip(array_flip($test)); $time=microtime(true)-$time; echo ‘Flip Flip: ‘.$time.”\n”; Output: Array Unique: 1.1829199790955 Keys Flip: 0.0084578990936279 Flip … Read more
It’s because array_unique compares items using a string comparison. From the docs: Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used. The string representation of an array is simply the word Array, no matter … Read more
Easiest way would be to put them into a new array either via a loop, or better yet array_values function. $new_array = array_values($original_array) More information
The array_unique function will do this for you. You just needed to add the SORT_REGULAR flag: $items_thread = array_unique($items_thread, SORT_REGULAR); However, as bren suggests, you should do this in SQL if possible.