note 73138 deleted from ref.array by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 19:28:05 +0000 |
| Subject: | note 73138 deleted from ref.array by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134520@lists.php.net to get a copy of this message | ||
Note Submitter: Colin
----
I couldn't get the previous set pointer functions working for whatever reason so I modifed. I
trigger errors if needed but obviously you can do whatever
// setPointer - Sets the pointer to the provided $key in provided $array
final private function setPointer(&$array, $set_key) {
// Check if $array is an array
if (!is_array($array)) {
trigger_error('$array must be an array', E_USER_ERROR);
}
// Check if $key is in $array
if (!array_key_exists($set_key, $array)) {
trigger_error('$set_key must exist in $array', E_USER_ERROR);
}
// Set array pointer to first element
reset($array);
// Cycle through array
while ($set_key != key($array)) {
// Advance the pointer
next($array);
}
}
Try with:
$array = array();
$array[0] = 'zero';
$array[1] = 'one';
$array[2] = 'two';
$array[3] = 'three';
// Set the pointer
setPointer($array, 1);
// Display elements
echo current($array); // Outputs 'one'
echo next($array); // Outputs 'two'