Jump to content

Starting an array from an x number and relooping back


rbrown

Recommended Posts

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,

<?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>';
?>

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>';
?>

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,

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.