note 92301 deleted from function.current by danbrown
| From: | danbrown@php.net | Date: | Sat, 18 Jul 2009 00:31:21 +0000 |
| Subject: | note 92301 deleted from function.current by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158446@lists.php.net to get a copy of this message | ||
Note Submitter: ratfactor at gmail dot com
----
**Apologies. Please ignore or remove my previous note. This version allows for false values in the
array.**
In the rare need for look-ahead and look-behind ability while looping through an array, you can use
prev(), next() and current() like so:
<?php
$nums = array('a','b','c',false,'e', 'f');
for(reset($nums), $i=0; $n=current($nums), key($nums)!==null; next($nums), $i++){
// look behind
$behind=' ';
if($i>0){
$n=prev($nums);
$behind=$n;
$n=next($nums);
}
// look ahead
$ahead=' ';
next($nums);
if(key($nums)!==null){
$ahead=current($nums);
}
prev($nums);
echo "$i: $behind $n $ahead\n";
}
?>
Results:
0: a b
1: a b c
2: b c
3: c e
4: e f
5: e f