Thomisback Posted January 16, 2009 Share Posted January 16, 2009 Hi, I'm having a really interesting problem, after searching for some time I decided to post it here. For example if I have an array like this: //Creates an array with elements. $theVariable = array("1", "5", "8", "15", "16", "17", "23", "44", "45"); How would I change it so there are no more consecutive numbers? Like this: //Creates an array with elements. $theVariable = array("1", "5", "8", "16", "23", "44"); Suggestions are welcome... Kind regards Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/ Share on other sites More sharing options...
RussellReal Posted January 16, 2009 Share Posted January 16, 2009 I seem to notice in the bottom example yoru script favors 16 over 15 and 44 over 45 is there any specific meaning for this? Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738185 Share on other sites More sharing options...
Thomisback Posted January 16, 2009 Author Share Posted January 16, 2009 Oh my bad, No I was just showing that it should leave one of the numbers of the consecutive numbers instead of deleting all of them. Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738187 Share on other sites More sharing options...
RussellReal Posted January 16, 2009 Share Posted January 16, 2009 what about recurring? like 1 1 1 1 and should this be alpha numeric or just numeric? Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738188 Share on other sites More sharing options...
Thomisback Posted January 16, 2009 Author Share Posted January 16, 2009 There will never be recurring numbers so no need to worry about that. All of the values in the array will be numeric. Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738189 Share on other sites More sharing options...
RussellReal Posted January 16, 2009 Share Posted January 16, 2009 try that, and I just gave a lecture about giving out scripts :'( I'm a hypocrite but this was fun <?php $array = array(1,3,4,7,8,12,13,14); foreach ($array as $k => $v) { if (isset($prev)) { if (($v - $prev) != 1) $newArray[] = $v; } else { $newArray[] = $v; } $prev = $v; } print_r($newArray); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738191 Share on other sites More sharing options...
Thomisback Posted January 16, 2009 Author Share Posted January 16, 2009 Haha thanks I just tried it, the array in your example: $array = array(1,3,7,12,13,14); Should return something like: $array = array(1,3,4,7,12,14); But gives me: Array ( [0] => 1 [1] => 3 [2] => 7 [3] => 12 ) Any help? Never mind I think it works Let me try that again XD Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738192 Share on other sites More sharing options...
Thomisback Posted January 16, 2009 Author Share Posted January 16, 2009 RussellReal I love you XD Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738195 Share on other sites More sharing options...
chronister Posted January 16, 2009 Share Posted January 16, 2009 Just to play the devil's advocate here.... What about an array that was like this.... $numbers = array(1, 6, 7, 3, 2, 5, 66, 22, 78); Are you already sorting the array somewhere in your code, or does the array get created in a way that there will *never* be larger numbers before smaller numbers or consecutive numbers that are not next to each other in the array? nate Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738196 Share on other sites More sharing options...
Thomisback Posted January 16, 2009 Author Share Posted January 16, 2009 The values in the array are sorted from small to big so that shouldn't be a problem Quote Link to comment https://forums.phpfreaks.com/topic/141039-solved-check-if-array-contains-consecutive-numbers/#findComment-738198 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.