Home >Backend Development >PHP Tutorial >Example analysis of using pointers for function operations in PHP

Example analysis of using pointers for function operations in PHP

黄舟
黄舟Original
2017-12-06 10:11:172999browse

The internal pointer of the array is the internal organization mechanism of the array, pointing to an element in an array. The default is to point to the first element in the array. By moving or changing the position of the pointer, you can access any element in the array. Regarding the control of array pointers, today we will introduce to you an example analysis of PHP using pointers to perform function operations!

PHP provides the following built-in functions that can be used.

current(): Get the content data of the current pointer position.
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 unit.
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.

These functions have only one parameter, which is the array itself to be operated on. In the following example, these array pointer functions are used to control the order in which elements in an array are read. The code is as follows:

< ?php
 $contact = array(
 "ID" => 1,
 "姓名" => "高某",
 "公司" => "A公司",
 "地址" => "北京市",
 "电话" => "(010)98765432",
 "EMAIL" => "gao@brophp.com",
 );
  
 //数组刚声明时,数组指针在数组中第一个元素位置
echo &#39;第一个元素:&#39;.key($contact).&#39; => &#39;.current($contact).&#39;<br>&#39;; //第一个元素
echo &#39;第一个元素:&#39;.key($contact).&#39; => &#39;.current($contact).&#39;<br>&#39;; //数组指针没动
 
next($contact);
 next($contact);
 echo &#39;第三个元素:&#39;.key($contact).&#39; => &#39;.current($contact).&#39;<br>&#39;; //第三个元素
 
end($contact);
 echo &#39;最后一个元素:&#39;.key($contact).&#39; => &#39;.current($contact).&#39;<br>&#39;;
  
 prev($contact);
 echo &#39;倒数第二个元素:&#39;.key($contact).&#39; => &#39;.current($contact).&#39;<br>&#39;;
  
 reset($contact);
 echo &#39;又回到了第一个元素:&#39;.key($contact).&#39; => &#39;.current($contact).&#39;<br>&#39;;
 ?>

Summary:

In the above example, the functions next(), prev(), and end are controlled by using pointers () and reset() to freely move the pointer position in the array, and then use the key() and current() functions to obtain the key and value of the current position in the array. I hope it will be helpful to your work!

Related recommendations:

Example of using array pointer functions to operate arrays in PHP

The PHP implementation of the Joseph ring problem uses PHP array internal pointer operation functions

PHP array traversal knowledge summary (including traversal methods , array pointer operation function, array traversal speed test)

The above is the detailed content of Example analysis of using pointers for function operations in PHP. 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