note 73138 added to ref.array
| From: | Colin at osu1 dot php dot net | Date: | Sat, 10 Feb 2007 05:31:38 +0000 |
| Subject: | note 73138 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123210@lists.php.net to get a copy of this message | ||
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'
----
Server IP: 216.194.113.175
Probable Submitter: 71.234.177.249
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/73138
Del: integrated -- https://master.php.net/note/delete/73138/integrated
Del: useless -- https://master.php.net/note/delete/73138/useless
Del: bad code -- https://master.php.net/note/delete/73138/bad+code
Del: spam -- https://master.php.net/note/delete/73138/spam
Del: non-english -- https://master.php.net/note/delete/73138/non-english
Del: in docs -- https://master.php.net/note/delete/73138/in+docs
Del: other reasons-- https://master.php.net/note/delete/73138
Reject -- https://master.php.net/note/reject/73138
Search -- https://master.php.net/manage/user-notes.php