john-formby Posted March 19, 2013 Share Posted March 19, 2013 Hi, I am trying to develop a room allocation application, but am struggling a bit to get my head around the coding. I am trying to break it down into small chunks and then maybe see if I can get everything to work together. I have a mysql table which contains a list of users. I have returned the results of the user IDs from my select query into an array. I also have another table which holds a numeric marker. The marker will change. My array of user IDs would look like: Array ( [0] => 1 [1] => 3 [2] => 5 ) My numeric marker could be: 1 What I need to do is do a complete loop through my array starting at the numeric marker position. This would return: [1]=>3 [2]=>5 [0]=>1 As you can see, it jumps back to the beginning of the array and outputs the first element to complete the loop. Depending on the numeric marker and this particular data set, the array could also run like: Marker = 0: [0]=>1 [1]=>3 [2]=>5 Marker = 2: [2]=>5 [0]=>1 [1]=>3 I have had a go at writing the code and if I start at numeric marker 1, I can get it to run to the end of the array. I just have no idea how to reset back to the beginning and run until I reach numeric marker minus 1. Here is what I have so far: $i = $numericMarker; foreach ($userID as $key => $value) { if ($key < $numericMarker) continue; echo '$i = '.$i.' $value = '.$value.'<br />'; $i++; } Any help you can give me would be greatly appreciated. Many thanks, John Quote Link to comment https://forums.phpfreaks.com/topic/275849-iterate-once-through-array-starting-part-way-through/ Share on other sites More sharing options...
PaulRyan Posted March 19, 2013 Share Posted March 19, 2013 (edited) This sounded pretty fun, so I took a stab at it. Try this: <?PHP $array = array(1,3,5); $arrayLength = count($array); $outputArray = array(); $marker = 1; for($i=0; $i<$arrayLength; $i++) { if($marker >= $arrayLength) { $marker = 0; } $outputArray[] = $array[$marker]; $marker++; } echo '<pre>'; print_r($outputArray); Edited March 19, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/275849-iterate-once-through-array-starting-part-way-through/#findComment-1419529 Share on other sites More sharing options...
Solution trq Posted March 19, 2013 Solution Share Posted March 19, 2013 Another option: <?php$a = [1,3,5];$marker = 1;$new = array_merge(array_splice($a, $marker), array_splice($a, 0, $marker));foreach ($new as $val) { echo "$val\n";} Quote Link to comment https://forums.phpfreaks.com/topic/275849-iterate-once-through-array-starting-part-way-through/#findComment-1419534 Share on other sites More sharing options...
PaulRyan Posted March 19, 2013 Share Posted March 19, 2013 Another option: <?php$a = [1,3,5];$marker = 1;$new = array_merge(array_splice($a, $marker), array_splice($a, 0, $marker)); foreach ($new as $val) {echo "$val\n";} I had a strong feeling there was a way to do this using array functions, was browsing through them just now, beat me too it. Quote Link to comment https://forums.phpfreaks.com/topic/275849-iterate-once-through-array-starting-part-way-through/#findComment-1419536 Share on other sites More sharing options...
john-formby Posted March 19, 2013 Author Share Posted March 19, 2013 Wow, thank you very much for your replies. PaulRyan, I can follow the logic of yours and I understand what you have done, so thank you. Trq, I love how short your code is, but I am unfamiliar with those array functions. I will be heading off to php.net to check them out now Thanks to both of you, John Quote Link to comment https://forums.phpfreaks.com/topic/275849-iterate-once-through-array-starting-part-way-through/#findComment-1419561 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.