Jump to content

Iterate once through array, starting part way through


Go to solution Solved by trq,

Recommended Posts

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

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 by PaulRyan

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.