What is php array pointer

zbt
Release: 2023-06-12 10:24:46
Original
1128 people have browsed it

php array pointer is an internal organizational mechanism of the array that points to an element in an array. Each array has an internal pointer pointing to it. The pointer initially points to the first element of the current array. By moving Or change the position of the pointer to access any element in the array.

What is php array pointer

The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.

In PHP, the array pointer is the internal organizational mechanism of the array that points to an element in an array.

Each array has an internal pointer pointing to its "current" unit (element). This pointer initially points to the first unit in the current array.

You can access any element in the array by moving or changing the position of the pointer. For controlling array pointers, PHP provides the following built-in functions that can be used.

current(): Returns the current element of the array

pos(): An alias for current, the usage is the same.

key(): Read the index value (key value) of the data pointed to by the current pointer.

next(): Move the internal pointer in the array to the next cell.

prev(): Rewind the internal pointer of the array by one bit.

end(): Point the internal pointer of the array to the last element.

reset(): Unconditionally moves the current pointer to the first index position.

current() function

The parameter is an array, passed by reference, and returns the value pointed to by the current array pointer. If the pointer moves beyond the end of the array, it returns false. If the array is empty In this case, return false

The pointer of the array is at the first position of the array by default

<?php
$arr = [11,22,33,44,55,66];
echo current($arr);
?>
Copy after login

What is php array pointer

key() function

The parameter is an array , passing by reference, returns the key of the element pointed to by the current pointer. When there is no element in the bit pointed by the pointer, null is returned. An empty array returns null

<?php
$arr = [11,22,33,44,55,66];
var_dump($arr);
echo current($arr)."<br>";   //当前指针指向11 
echo key($arr)."<br>";   //当前元素的键名
?>
Copy after login

What is php array pointer

next( ) Function

The parameter is an array, pass by reference, move the array pointer to the next bit, and return the value of that bit. When there is no element in the bit, it returns false. An empty array will also return false

<?php
$arr = [11,22,33,44,55,66];
echo current($arr)."<br>";   //指针指向11
next($arr);                    //指针指向22
echo current($arr)."<br>";   //指针指向22
next($arr);                    //指针指向33
next($arr);                    //指针指向44
echo current($arr)."<br>";   //指针指向44
?>
Copy after login

What is php array pointer

prev() function

The parameter is an array, passed by reference, moves the array pointer to the previous bit, and returns the value of that bit. When there is no element in that bit Returns false when an empty array returns false The pointer moves to the position of the last element of the array, and then returns the value of the last element. If the array is empty, it returns false

<?php
$arr = [11,22,33,44,55,66];
echo current($arr)."<br>";   //指针指向11
next($arr);                    //指针指向22
next($arr);                    //指针指向33
echo current($arr)."<br>";   //指针指向33
prev($arr);                    //指针指向22
echo current($arr)."<br>";   //指针指向22
?>
Copy after login

What is php array pointer

reset() function

The parameter is an array, passed by reference, resets the internal pointer of an array to the first position, which is the position of the first element of the array, and then returns the value of the first element, if the array is empty Return false

<?php
$arr = [11,22,33,44,55,66];
echo current($arr)."<br>";   //指针指向11
end($arr);                 //指针指向11
echo current($arr)."<br>";   //指针指向66
?>
Copy after login

What is php array pointer

The above is the detailed content of What is php array pointer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template