Voting

: nine minus three?
(Example: nine)

The Note You're Voting On

andy at andysdrawings dot co dot uk
6 years ago
brentimus' array_set_pointer function will only work if the array value is unique in the array, and none of the array values are FALSE. It would be more reliable to use key() instead of current(). For similar reasons it's better to check key() after calling next() to determine whether the next() element "exists". Simply checking the value returned by next() will produce a false negative when looking at, for example, the first element of the array: ['one', 0, 'three']

However, it also turns out that the copied array retains the original array's pointer, so array_set_pointer is not actually required here. The following should work:

<?php
function has_next(array &$array) {
$A_work = $array; // $A_work is a copy of $array including its internal pointer.
next($A_work);
if (
key($A_work) === NULL)
return
false;
else
return
true;
}
?>

<< Back to user notes page

To Top