开发者

Display array values not repeadly

I have the following array

$ar = array ( 1,2,3,4,5,3,6,7,...)

i do a foreach to display elements like开发者_JAVA技巧

$i = 0
foreach ($ar as $tab){

echo $tab[i];

$i++
}

I dont want to display twice the same value like 3. i just want 1 2 3 4 5 6 7...


You can use array_unique to get the unique set of values from your array before iterating over it:

$arr = array(1,2,3,4,5,3,6,7);
foreach (array_unique($arr) as $tab)
{
  // ...
}


try array_unique


You may use flip-flip-merge technique which is faster than array_unique()

$ar = array(1,2,3,4,5,3,6,7);
$ar = array_merge(array_flip(array_flip($ar)));

You may read about it here http://thebatesreport.com/blog/?p=9

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜