note 52362 added to function.next
| From: | brentimus at osu1 dot php dot net | Date: | Thu, 28 Apr 2005 17:10:22 +0000 |
| Subject: | note 52362 added to function.next | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88706@lists.php.net to get a copy of this message | ||
Papipo's function below is usefull in concept but does not work.
"Since you do not pass the array by reference, its pointer is only moved inside the
function."
This is true, but the array you are manipulating in your has_next() function will have it's
pointer set to the first element, not the same position as the original array. What you want to do
is pass the array to the has_next() function via reference. While in the has_next() function, make a
copy of the array to work on. Find out the current pointer position of the original array and set
the pointer on the working copy of the array to the same element. Then you may test to see if the
array has a "next" element.
Try the followig insetad:
<?php
function has_next(&$array)
{
$A_work=$array; //$A_work is a copy of $array but with its internal pointer set to the first
element.
$PTR=current($array);
array_set_pointer($A_work, $PTR);
if(is_array($A_work))
{
if(next($A_work)===false)
return false;
else
return true;
}
else
return false;
}
function array_set_pointer(&$array, $value)
{
reset($array);
while($val=current($array))
{
if($val==$value)
break;
next($array);
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.next.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52362
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52362&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52362&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52362&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52362&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52362&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52362&report=yes
Search -- http://master.php.net/manage/user-notes.php