Home  >  Article  >  Backend Development  >  What function does php use to output the current element of an array?

What function does php use to output the current element of an array?

青灯夜游
青灯夜游Original
2022-06-27 20:27:261446browse

php uses the "current()" function to output the current element of the array; the current() function can return the current element of the array, and the syntax is "current($array)". In PHP, each array has an internal pointer pointing to its "current" unit (element); and through the current() function, you can get the value of the array element pointed to by the current internal pointer.

What function does php use to output the current element of an array?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php output array current Elements use the "current()" function.

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

Through the current() function, you can get the value of the element pointed to by the internal pointer. Its syntax format is as follows:

current($array)
  • The parameter $array represents the array being operated on.

current() function can return the value of the array element pointed to by the current internal pointer, but it does not move the pointer. If you need to move the pointer, you need to use it in conjunction with other functions; if the internal The pointer points beyond the end of the array, and the current() function returns FALSE.

Example: Use the current() function to output the current element of the array

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
$array = array(
    &#39;PHP中文网&#39;,
    &#39;https://www.php.cn/&#39;,
    &#39;current() 函数&#39;
    );
   
    echo current($array).&#39;<br>&#39;;
echo pos($array).&#39;<br>&#39;;
next($array);
echo current($array).&#39;<br>&#39;;
next($array);
next($array);
var_dump(current($array));
?>

What function does php use to output the current element of an array?

In the above example code we used the next() function, its function is the same as The current() function is similar, but it can move the internal pointer down to the next element of the array. You can learn about it here.

In PHP, there are many functions similar to the current() function that can operate the internal pointers of the array, as follows:

  • end(): end the internal pointer Points to the last element in the array;

  • next(): Points the internal pointer to the next element in the array;

  • prev() : Point the internal pointer to the previous element in the array;

  • reset(): Point the internal pointer to the first element in the array;

  • each(): Returns the key name and key value of the current element, and points the internal pointer to the next element in the array.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What function does php use to output the current element of an array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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