babaz Posted November 11, 2010 Share Posted November 11, 2010 ok so i have an array which has duplicate values in it. i used array_unique() to remove duplicate values. the array had values 1,2,2,3. after the following code: $unique = array_unique($suggest); $size = count($unique); for ($x=0;$x<=$size;$x++) { echo $unique[$x]; echo "<br>"; } it prints out 1,2 and then gives an error, "Notice: Undefined offset: 2 in E:\wamp\www\Copy of pro2\Classes.php on line 1074" and then finally 3. any ideas how i can solve this. im assuming that array_unique() assigns the index 0,1,3 of the duplicate value array to unique array at the same indexs 0,1,3. since the index 2 of duplicate array is a duplicate value it is ommited but the unique array does not have an array at index 2. Quote Link to comment https://forums.phpfreaks.com/topic/218338-unique-array/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 11, 2010 Share Posted November 11, 2010 for ($x=0;$x<=$size;$x++) ^^^ The $x = $size element does not exist. Only the index values $x<$size exist. Quote Link to comment https://forums.phpfreaks.com/topic/218338-unique-array/#findComment-1132846 Share on other sites More sharing options...
babaz Posted November 11, 2010 Author Share Posted November 11, 2010 thats not the problem, ive tried that. with that line of code it prints out, 1,2, and then the error notice. it does not print out 3. Quote Link to comment https://forums.phpfreaks.com/topic/218338-unique-array/#findComment-1132849 Share on other sites More sharing options...
babaz Posted November 11, 2010 Author Share Posted November 11, 2010 as an exaple try running this code and see wot happens: <?php $dirty = array('danish','mukesh','braden1','shadab','bev','braden1','kirsten','john'); $clean = array_unique($dirty); for($i=0; $i<count($clean); $i++) { echo $clean[$i] . "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/218338-unique-array/#findComment-1132850 Share on other sites More sharing options...
requinix Posted November 11, 2010 Share Posted November 11, 2010 Seriously guys. The PHP manual is online, it's free, and it's always available. Don't waste it. array_unique Note that keys are preserved. $unique = array_values(array_unique($suggest)); PFMaBiSmAdPosted is still right though: Quote Link to comment https://forums.phpfreaks.com/topic/218338-unique-array/#findComment-1132854 Share on other sites More sharing options...
babaz Posted November 11, 2010 Author Share Posted November 11, 2010 thanks....dat helped! also tried for ($x=0;$x<$size;$x++) { if(isset($unique[$x])) { echo $unique[$x]; echo "<br>"; } } and worked fine.... Quote Link to comment https://forums.phpfreaks.com/topic/218338-unique-array/#findComment-1132857 Share on other sites More sharing options...
Psycho Posted November 11, 2010 Share Posted November 11, 2010 thanks....dat helped! also tried for ($x=0;$x<$size;$x++) { if(isset($unique[$x])) { echo $unique[$x]; echo "<br>"; } } and worked fine.... Yeah, but that is still a flawed solution. The problem is that your original array looks like this array ( 0 -> 1, 1 -> 2, 2 -> 2, 3 -> 3 ) After using array_unique() you are left with array ( 0 -> 1, 1 -> 2, 3 -> 3 ) ... because the keys are preserved. Therefor there is no element with an index of 2. You can either reset the array indexes as requinix proposed. But, there is an even easier solution - DON'T USE A FOR LOOP. All you want to do is iterate through each element in an array. So, just use foreach() - that's what it is for. $unique = array_unique($suggest); foreach($unique as $value) { echo "{$value}<br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/218338-unique-array/#findComment-1132863 Share on other sites More sharing options...
kenrbnsn Posted November 11, 2010 Share Posted November 11, 2010 Here's another solution that doesn't even use a loop: <?php $suggest = array(1,2,2,3); echo implode("<br>\n",array_unique($suggest)) . "<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/218338-unique-array/#findComment-1132888 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.