tryingtolearn Posted February 4, 2009 Share Posted February 4, 2009 Maybe I am missing it but say you have an array that contains 15 numbers array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) when a page loads it is going to use 5 of the numbers at random out of the array, could be 1 - 2 - 6 - 9 - 11 or 8 - 11 - 12 - 13 -14 but what I need to find is if those #s are in any consecutive order ex 1-2-3-4-5 or 7-8-9-10-11 Ive read through the array function page but dont see a anything for this, is there an array function that does this? Quote Link to comment https://forums.phpfreaks.com/topic/143707-solved-does-an-array-function-like-this-exist/ Share on other sites More sharing options...
genericnumber1 Posted February 4, 2009 Share Posted February 4, 2009 Something like this would work: <?php function isConsecutive($array) { $previous = array_shift($array); foreach($array as $value) { if($value != $previous + 1) { return false; } $previous = $value; } return true; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143707-solved-does-an-array-function-like-this-exist/#findComment-754016 Share on other sites More sharing options...
tryingtolearn Posted February 4, 2009 Author Share Posted February 4, 2009 Thanks genericnumber1 I will try it out. Quote Link to comment https://forums.phpfreaks.com/topic/143707-solved-does-an-array-function-like-this-exist/#findComment-754022 Share on other sites More sharing options...
trq Posted February 4, 2009 Share Posted February 4, 2009 What do you want to do if there not in consecutive order? What if your returned values start at 13? Do you want it to go 13,14,15,1,2 what? We need more details. Quote Link to comment https://forums.phpfreaks.com/topic/143707-solved-does-an-array-function-like-this-exist/#findComment-754023 Share on other sites More sharing options...
Andy-H Posted February 4, 2009 Share Posted February 4, 2009 Something like this would work: <?php function isConsecutive($array) { $array = array_shift($array); $array = array_chunk($array, 5); $first = true; foreach($array as $value) { if ( isSet($first) ){ $previous = $value - 1; $first = false; } if($value != $previous + 1) { return false; } $previous = $value; } return true; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143707-solved-does-an-array-function-like-this-exist/#findComment-754028 Share on other sites More sharing options...
.josh Posted February 4, 2009 Share Posted February 4, 2009 function isConsecutive($array) { if (!is_array($array) return false; sort($array); $min = reset($array); $max = end($array); if ($array == range($min, $max)) return true; return false; } // end isConsecutive Quote Link to comment https://forums.phpfreaks.com/topic/143707-solved-does-an-array-function-like-this-exist/#findComment-754038 Share on other sites More sharing options...
tryingtolearn Posted February 4, 2009 Author Share Posted February 4, 2009 Thanks Crayon Violet That worked also Thorpe - If it returns true it is going to 1 set of additional checks, if false it goes to a different set of checks, it is sorting the array so even if it starts at 13 - its just going to take the five integers and see if they are in consecutive order. Thanks for the help, once again seems like I was making harder than it needed to be haha Quote Link to comment https://forums.phpfreaks.com/topic/143707-solved-does-an-array-function-like-this-exist/#findComment-754181 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.