March 29, 2012
DISTINCT for foreach
Question by Dirk Fograust
<?php
$array = array('aaa', 'bbb', 'aaa', 'ccc', 'ddd', 'ccc', 'eee');
foreach($array as $a){
echo $a;
}
Is possible use some like DISTINCT for foreach? I would like show each values only one, without repeat. How is the best way for this?
Answer by tereško
Actually array_unique()
gets pretty bad when you have large arrays. You would be better off with $uniques = array_flip(array_flip($array))
.
Answer by Starx
Use array_unique()
$array = array('aaa', 'bbb', 'aaa', 'ccc', 'ddd', 'ccc', 'eee');
$result = array_unique($array);
print_r($result);