How to forward array pointer?

Hi guys,

I have a foreach(), I wanted to forward the array pointer but it won’t work.
Below is what I have done so far.


 	function next_arr() {
		$transport = array(
						'num1' => 'one',
						'num2' => 'two',
						'num3' => 'three',
						'num4' => 'four',
						'num5' => 'five', 
						'num6' => 'six',
						'num7' => 'seven', 
						'num8' => 'eight',
						'num9' => 'nine',
						'num10' => 'ten');
		
		foreach($transport as $transpo) {
			echo $transpo .'<br>';
			next($transport);			
		}		

So how do I forward the array pointer inside foreach() using next()?

Thank you very much in advanced.

When you use a foreach it will work its way through each element in an array in turn. Are you trying to do something according to what the data in the element that it’s processing?

<?php
$transport = array(
			'num1' => 'one',
			'num2' => 'two',
			'num3' => 'three',
			'num4' => 'four',
			'num5' => 'five',
			'num6' => 'six',
			'num7' => 'seven',
			'num8' => 'eight',
			'num9' => 'nine',
			'num10' => 'ten'
			);


function next_arr($transport) {
		
		echo  current($transport) . '<br>';
		next($transport);
		
}



next_arr($transport);
foreach ($transport as $key => $value){        	
		next_arr($transport);	
}


@SpacePhoenix

I understand how foreach() works.
I just wanted to control foreach() pointer so it will forward the array pointer.

@Stride65
Thanks due.
But it doesn’t work.

The short answer is: you don’t.

Calling next() within a foreach will have no effect, as you have seen. This is because foreach() does not use the same “array pointer” that functions like current(), next(), reset(), etc. use. This can clearly be seen with an example like below:


$letters = range('a', 'h');
foreach ($letters as $letter) {
    echo current($letters), PHP_EOL;
}

/* Outputs:
b
b
b
b
b
b
b
b
*/

To get the effect of moving the array pointer forward there are several options (below are only examples, there are many more ways to get the same result).

  1. foreach() with continue
  2. foreach() only what you need
  3. for() loop

foreach() with continue


$print = true;
foreach ($letters as $letter) {
    if ($print = !$print) {
        continue;
    }
    echo $letter, PHP_EOL;
}

foreach() only what you need

There are many ways to do this but the idea is to only loop over the items you want to be looping over.


// Get every 2nd key
$keys = array_map('reset', array_chunk(array_keys($letters), 2));
foreach ($keys as $key) {
    echo $letters[$key], PHP_EOL;
}

for() loop

This is most like what you originally tried to do, but uses for() to iterate over the array manually.


for (reset($letters); key($letters) !== null; next($letters)) { // basically a foreach() loop
    echo current($letters), PHP_EOL;
    next($letters); // Skip next letter
}

Hopefully that will give you some ideas.

@Salathe

I have formulated my own codes,


	<?php
		$j=1;
		if(!empty($records)) {
			foreach($records as $record) {			
				//for($j=1; $j<=2; $j++) {
					if($j>2) {
						$j=1;						
					}
					if($j==1) {
						echo "<div class='row'>";
					}
						if($j<=2) {
							echo "<div class='span6'>";
						}
	?> 

				        	<div id="contee" align="left">
								<table border="0">
									<tr>
										<td align="left">
											<a href="<?php echo $record["title_url"]; ?>">
												<?php echo '<h2>'. $record['title'] .'</h2>'; ?>
											</a>
										</td>
									</tr>							
								</table>				        		
				        	</div>

	<?php
						if($j<=2) {
							echo "</div>";
							//forward one step array pointer
							//next($records);
						}
					if($j==2) {
						echo "</div>";
					}

					$j++;
				//}
			}
		}
	?> 

@salathe
Your answers is very nice dude.
Thanks anyway.