Skip to content

Latest commit

 

History

History
162 lines (105 loc) · 3.4 KB

File metadata and controls

162 lines (105 loc) · 3.4 KB

PHP Array Pointers

PHP array pointer functions allow you to manipulate and traverse arrays manually using internal pointers.


Learning Objectives

  • Understand PHP's internal array pointer and the functions that move it — current(), next(), prev(), reset(), end().
  • Traverse an array manually with a while loop instead of foreach.
  • Know when to call reset() to avoid stale-pointer bugs when reusing an array.

pointers.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Array Pointers</title>
</head>
<body>

    <?php
        $array1 = array(45, 41, 52, 98, 78, 50, 43, 11, 56);

        print_r($array1); echo "<br />";

        echo current($array1) . "<br />"; // 45

        next($array1);

        echo current($array1) . "<br />"; // 41

        next($array1); // 52

        next($array1); // 98

        next($array1); // 78

        echo current($array1) . "<br />"; // 78

        prev($array1); // 98

        prev($array1); // 52

        echo current($array1) . "<br />"; // 52

        reset($array1); // back to 45

        echo current($array1) . "<br />"; // 45

        next($array1);

        echo current($array1) . "<br />"; // 41

        end($array1);

        echo current($array1) . "<br />"; // 56

    ?>

</body>
</html>

Output

pointers.php

Array ( [0] => 45 [1] => 41 [2] => 52 [3] => 98 [4] => 78 [5] => 50 [6] => 43 [7] => 11 [8] => 56 )
45
41
78
52
45
41

pointers2.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Pointers</title>
</head>
<body>

    <?php
        $array1 = array(1, 2, 3, 4, 5, 5, 6, 7, 8, 9);

        while ($var1 = current($array1)) {
            echo $var1 . ",";
            next($array1);
        }
    ?>

</body>
</html>

Output

pointers2.php

1,2,3,4,5,5,6,7,8,9,

Key Functions

Function Description
current() Returns the current element in the array
next() Moves the pointer to the next element
prev() Moves the pointer to the previous element
reset() Moves the pointer to the first element
end() Moves the pointer to the last element

Notes

  • These functions do not loop automatically; they require manual control.

  • Very useful when you need sequential access with logic at each step (e.g., while loops).

  • Use reset() before traversing an array if the pointer has moved.


Best Practice

Always reset the pointer before reusing the array with functions like current() or next() to avoid unexpected results.


Common Mistakes

  • Using while ($v = current($arr)) as a loop condition — it stops early on any falsy element (0, '', '0', false, null), not at the end of the array. Use while (key($arr) !== null) or a foreach loop for value-agnostic iteration.
  • Forgetting the pointer is per-array internal state: passing an array to a function by value operates on a copy, so pointer moves inside the function do not affect the caller's array.
  • Not calling reset() before re-traversing an array whose pointer has already been advanced.

Related