mrmos Posted August 28, 2009 Share Posted August 28, 2009 Does anyone know if there is a php function to check if a set of numbers are in sequence? For example 1,2,3,4,5 or 23,24,25. If there isn't a function to do this could anyone suggest the best way to do this? Thanks Link to comment https://forums.phpfreaks.com/topic/172327-check-number-sequence/ Share on other sites More sharing options...
lemmin Posted August 28, 2009 Share Posted August 28, 2009 You could do this: if(array_intersect($needle, $haystack) == $needle) echo "Sequence is in array"; That matches even if they are out of order, though. If you want to match the order too, you can just keep them as a string (or put them into a string) and simply match it with strstr or regex. Link to comment https://forums.phpfreaks.com/topic/172327-check-number-sequence/#findComment-908602 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 I think this should do the trick: function checkSequence($array) { $lowest = min($array); $inSequence = true; $sizeof = sizeof($array); for ($i = 0; $i < $sizeof; ++$i) { if ($lowest !== $array[0]) { $inSequence = false; break; } else if (isset($array[$i + 1]) && $array[$i] > $array[$i + 1]) { $inSequence = false; break; } } } return $inSequence; } Link to comment https://forums.phpfreaks.com/topic/172327-check-number-sequence/#findComment-908610 Share on other sites More sharing options...
lemmin Posted August 28, 2009 Share Posted August 28, 2009 Oh woops. My brain added an 'a' in between your "in" and "sequence." Now you know how to check if a set of numbers are in a sequence, though. Link to comment https://forums.phpfreaks.com/topic/172327-check-number-sequence/#findComment-908624 Share on other sites More sharing options...
mrmos Posted August 28, 2009 Author Share Posted August 28, 2009 Nice function! Maybe there's a small bug in it as with the following two arrays it returns true? $a1= array(1,2,3,4,5,6); $a2= array(3,4,5,12,15); Link to comment https://forums.phpfreaks.com/topic/172327-check-number-sequence/#findComment-908625 Share on other sites More sharing options...
lemmin Posted August 28, 2009 Share Posted August 28, 2009 You could do it like this, too: function checkSequence($array) { $last = $array[0]; for($i=1;$i<sizeof($array);$i++) { if ($array[$i] != $last+1) return false; $last = $array[$i]; } return true; } Link to comment https://forums.phpfreaks.com/topic/172327-check-number-sequence/#findComment-908629 Share on other sites More sharing options...
mrmos Posted August 28, 2009 Author Share Posted August 28, 2009 Great stuff lemmin, that works a treat!! Link to comment https://forums.phpfreaks.com/topic/172327-check-number-sequence/#findComment-908635 Share on other sites More sharing options...
ignace Posted August 29, 2009 Share Posted August 29, 2009 An even simpler solution would be: if ($array == array_intersect(range(min($array), max($array)), $array))) { //in sequence } This will return true for: $a1= array(1,2,3,4,5,6); and returns false for: $a2= array(3,4,5,12,15); Link to comment https://forums.phpfreaks.com/topic/172327-check-number-sequence/#findComment-908837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.