BMAN99 Posted August 9, 2012 Share Posted August 9, 2012 Hey guys, I understand how to remove the duplicate values in an array using the "array_unique" function. But what if I want to remove only the duplicate values that occur "side by side"? For example, if this is my array... $array = array( 0 => 'Red', 1 => 'Blue', 2 => 'Blue', 3 => 'Green' ); ... then one of the "Blue" values should be removed. But if my array looks like this instead... $array = array( 0 => 'Red', 1 => 'Blue', 2 => 'Green', 3 => 'Blue' ); ... then I do not want any values removed, because the duplicate values do not occur next to each other in that scenario. So, anyone know of a simple way to do this? FYI, I will not know the size of the array or its values in advance. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/266870-how-do-you-remove-the-duplicate-values-in-an-array-in-this-particular-situation/ Share on other sites More sharing options...
Mahngiel Posted August 9, 2012 Share Posted August 9, 2012 how are you building the array? Quote Link to comment https://forums.phpfreaks.com/topic/266870-how-do-you-remove-the-duplicate-values-in-an-array-in-this-particular-situation/#findComment-1368151 Share on other sites More sharing options...
BMAN99 Posted August 9, 2012 Author Share Posted August 9, 2012 how are you building the array? By using the "explode" function. Quote Link to comment https://forums.phpfreaks.com/topic/266870-how-do-you-remove-the-duplicate-values-in-an-array-in-this-particular-situation/#findComment-1368152 Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 try $a = array (1,2,3,3,4,5,4); for ($i=1, $k=count($a); $i<$k; $i++) { if ($a[$i]==$a[$i-1]) { unset ($a[$i-1]); } } Quote Link to comment https://forums.phpfreaks.com/topic/266870-how-do-you-remove-the-duplicate-values-in-an-array-in-this-particular-situation/#findComment-1368156 Share on other sites More sharing options...
BMAN99 Posted August 9, 2012 Author Share Posted August 9, 2012 try $a = array (1,2,3,3,4,5,4); for ($i=1, $k=count($a); $i<$k; $i++) { if ($a[$i]==$a[$i-1]) { unset ($a[$i-1]); } } Perfect, Barand. Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/266870-how-do-you-remove-the-duplicate-values-in-an-array-in-this-particular-situation/#findComment-1368159 Share on other sites More sharing options...
jazzman1 Posted August 9, 2012 Share Posted August 9, 2012 One more from me. http://php.net/manual/en/function.next.php $transport = array('foot','foot','bike', 'car', 'plane','bike','bike','bike','car','foot'); $i = 0; while($i < count($transport)){ if($transport[$i] == next($transport)) unset ($transport[$i]); $i++; } echo '<pre>'.print_r($transport, true).'</pre>'; PS. I think, Brand's solution runs faster than mine, but....any way Quote Link to comment https://forums.phpfreaks.com/topic/266870-how-do-you-remove-the-duplicate-values-in-an-array-in-this-particular-situation/#findComment-1368184 Share on other sites More sharing options...
BMAN99 Posted August 10, 2012 Author Share Posted August 10, 2012 One more from me. http://php.net/manual/en/function.next.php $transport = array('foot','foot','bike', 'car', 'plane','bike','bike','bike','car','foot'); $i = 0; while($i < count($transport)){ if($transport[$i] == next($transport)) unset ($transport[$i]); $i++; } echo '<pre>'.print_r($transport, true).'</pre>'; PS. I think, Brand's solution runs faster than mine, but....any way Always nice to have options. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/266870-how-do-you-remove-the-duplicate-values-in-an-array-in-this-particular-situation/#findComment-1368487 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.