Dirk Fograust
Dirk Fograust

Reputation: 75

DISTINCT for foreach

<?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?

http://codepad.org/FZQNEBeK

Upvotes: 0

Views: 583

Answers (2)

tereško
tereško

Reputation: 58444

Actually array_unique() gets pretty bad when you have large arrays. You would be better off with $uniques = array_flip(array_flip($array)).

Upvotes: 1

Starx
Starx

Reputation: 78971

Use array_unique()

$array = array('aaa', 'bbb', 'aaa', 'ccc', 'ddd', 'ccc', 'eee');
$result = array_unique($array);
print_r($result);

Demo

Upvotes: 10

Related Questions