DamienRoche Posted December 14, 2008 Share Posted December 14, 2008 I have a simple array which will no doubt contain duplicates from time to time. It is within an if statement inside a function.... so, basically, I have: foreach($arr as $var){ echo "$var"; } What I want to do is have somekind of check to make sure I haven't already echoed the same var. An easier way would be to remove the duplicates first. How can I do this? I've tried array_unique($arr) before the foreach but isn't working so I must be missing something.. Anyone care to chip in? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/136924-solved-foreach-with-arraybut-dont-show-duplicates/ Share on other sites More sharing options...
sasa Posted December 14, 2008 Share Posted December 14, 2008 <?php $arr = array(1,2,1,3,2,1,4,3,5); $arr = array_unique($arr); foreach ($arr as $v){ echo $v,"<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136924-solved-foreach-with-arraybut-dont-show-duplicates/#findComment-715079 Share on other sites More sharing options...
Mark Baker Posted December 14, 2008 Share Posted December 14, 2008 Is your array just a simple array, e.g. $arr = array(1,3,5,2,4,6,1,5) or is it a more complex multi-dimensional array, e.g. $arr = array( 0 => array(1,2), 1 => array('sales',4)); Quote Link to comment https://forums.phpfreaks.com/topic/136924-solved-foreach-with-arraybut-dont-show-duplicates/#findComment-715083 Share on other sites More sharing options...
DamienRoche Posted December 14, 2008 Author Share Posted December 14, 2008 Thanks for the help guys..I sussed out I should have run it through a variable. Thankfully, Mark, it's a simple array. Those multidimensional arrays are horrific. To me at least. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/136924-solved-foreach-with-arraybut-dont-show-duplicates/#findComment-715087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.