rbrown Posted March 4, 2008 Share Posted March 4, 2008 I have an array with 40 elements and I want to be able to start at what ever element and have it loop though the array outputting to another array and have it continue back around to the where it started. What is happening is if I set, the "start" at 5, the "count" at 40, and then add "start plus count" together, and loop through using the "start plus count" as the stop number in the for loop, it returns only 35 elements. What do I have to do to get it to loop back around? Thanks, Link to comment https://forums.phpfreaks.com/topic/94353-starting-an-array-from-an-x-number-and-relooping-back/ Share on other sites More sharing options...
Barand Posted March 4, 2008 Share Posted March 4, 2008 <?php $data = range(1,40); $output = array(); $start = 5; $k = count ($data); for ($i=$start-1; $i < $k; $i++) $output[] = $data[$i]; for ($i=0; $i < $start-1; $i++) $output[] = $data[$i]; // check results echo '<pre>', print_r($output, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/94353-starting-an-array-from-an-x-number-and-relooping-back/#findComment-483221 Share on other sites More sharing options...
Barand Posted March 4, 2008 Share Posted March 4, 2008 or <?php $data = range(1,40); $output = array(); $start = 5; $output = array_merge (array_slice($data, $start-1), array_slice($data, 0, $start-1)); // check results echo '<pre>', print_r($output, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/94353-starting-an-array-from-an-x-number-and-relooping-back/#findComment-483223 Share on other sites More sharing options...
rbrown Posted March 4, 2008 Author Share Posted March 4, 2008 Sorry, That's not going to work... I should have told you what the array contains. the keys are alpha numeric and data is numbers. So you can't range the array. It can look something like this example: $test_start['Q']='18';$test_start['q']='19';$test_start['P']='20'; $test_start['p']='21';$test_start['O']='22';$test_start['o']='23'; $test_start['N']='24';$test_start['n']='25';$test_start['m']='26'; $test_start['M']='27';$test_start['L']='28';$test_start['l']='29'; $test_start['k']='30';$test_start['K']='31';$test_start['j']='32'; $test_start['J']='33';$test_start['I']='34';$test_start['i']='35'; $test_start['h']='37';$test_start['<']='55';$test_start['+']='56'; $test_start['~']='57';$test_start['}']='58';$test_start['{']='59'; $test_start['`']='60';$test_start['_']='61';$test_start['^']='62'; $test_start[']']='63';$test_start['[']='64';$test_start['@']='65'; $test_start['?']='66';$test_start[':']='67';$test_start['.']='69'; $test_start[',']='70';$test_start['*']='71';$test_start[')']='72'; etc... So how do I do it with that type of data? Thanks, Link to comment https://forums.phpfreaks.com/topic/94353-starting-an-array-from-an-x-number-and-relooping-back/#findComment-483236 Share on other sites More sharing options...
Barand Posted March 4, 2008 Share Posted March 4, 2008 try the second version that I posted Link to comment https://forums.phpfreaks.com/topic/94353-starting-an-array-from-an-x-number-and-relooping-back/#findComment-483247 Share on other sites More sharing options...
rbrown Posted March 4, 2008 Author Share Posted March 4, 2008 beauty... However my total count is off by 1... I have to run, but I'll figure it out when I get back. Thanks, Link to comment https://forums.phpfreaks.com/topic/94353-starting-an-array-from-an-x-number-and-relooping-back/#findComment-483293 Share on other sites More sharing options...
rbrown Posted March 6, 2008 Author Share Posted March 6, 2008 I figured it out... typo... Had two of the same keys with different elements Thanks for you help... Link to comment https://forums.phpfreaks.com/topic/94353-starting-an-array-from-an-x-number-and-relooping-back/#findComment-485173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.