coolguydudeman Posted January 16, 2011 Share Posted January 16, 2011 i am trying to make a range of numbers which have a step of 11 then 7 then 11 which can then be compared. the result im after is 1, 12, 19, 30. Is there a way i could do this without storing the sets in an array and looping though them? Any help would be greatly appreciated, thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 16, 2011 Share Posted January 16, 2011 Put them in separate arrays then use array_intersect() on all the array to generate an array with only the values that exist in all the arrays. Quote Link to comment Share on other sites More sharing options...
coolguydudeman Posted January 16, 2011 Author Share Posted January 16, 2011 Thanks for the reply mjdamato - I apologise for the bad explanation, I would like PHP to automatically create a number sequence starting at 1 and finishing at 30 with a step order of 11, 7, 11 without storing any information in arrays, I was thinking a for loop, but my attempts havent proven sucessful. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 16, 2011 Share Posted January 16, 2011 You have to store them somewhere to be able to compare them. What did you have mind if not an array? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 17, 2011 Share Posted January 17, 2011 number sequence starting at 1 and finishing at 30 with a step order of 11, 7, 11 I don't understand this, can you type out expected results for the number sequence Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2011 Share Posted January 17, 2011 Thanks for the reply mjdamato - I apologise for the bad explanation, I would like PHP to automatically create a number sequence starting at 1 and finishing at 30 with a step order of 11, 7, 11 without storing any information in arrays, I was thinking a for loop, but my attempts havent proven sucessful. Hmm... I guess I understand now. But, as Pikachu stated you would obviously need to to something with the values. Anyway, here is a simple loop to do as you ask. I've made it very flexible so you can change the parameters as needed. THis just echos the values to the page, but you can do with it what you want. //User configurable values $value = 1; //The start value $maxValue = 30; ??The maximum value before the loop will exit $stepValues = array(11, 7); //The values to be added on each step, you can add more values - will repeat //Non user configurable value $step = 0; //The loop while($value <= $maxValue) { echo "{$value}<br />\n"; $value += $stepValues[$step++%count($stepValues)]; } Quote Link to comment Share on other sites More sharing options...
coolguydudeman Posted January 17, 2011 Author Share Posted January 17, 2011 Thanks for that one mjdamato thats exactly what i was looking for! I appreciate the time you've taken to write that up for me. Quote Link to comment 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.