Home  >  Article  >  Backend Development  >  PHP array introductory tutorial: array pointer operations

PHP array introductory tutorial: array pointer operations

WBOY
WBOYOriginal
2016-07-25 08:57:511007browse
This article introduces the relevant content about array pointers in PHP arrays. Friends in need can refer to it.

In PHP, the functions involving array pointers are: reset, prev, end, next, current, and each.

Example 1, next and prev

<?php
//数组指针 next prev
$speed = range(0,220,20);
echo current($speed);//输出当前位置的值(在数组的开头位置)
$i = rand(1,11);
while($i--){
next($speed);//指针从当前位置向后移动一位
}
echo current($speed);//输出当前位置的值
echo "<br />";
echo prev($speed);//输出前一位置数组值
echo "<br />";
echo reset($speed);//重置数组的指针,将指针指向起始位置
echo "<br />";
echo end($speed);//输出最后位置的数组值
echo "<br />";
//by bbs.it-home.org
?>

Run results: 0220 200 0 220

Example 2, each function pointer operation

<?PHP
//数组指针
//each 函数
//by bbs.it-home.org
$speed = range(0,200,40);
echo "each实现指针下移 <br />";
echo "0挡的速度是".current(each($speed))."<br />";
echo "1挡的速度是".current(each($speed))."<br />";
echo "2挡的速度是".current(each($speed))."<br />";
echo "3挡的速度是".current(each($speed))."<br />";
echo "4挡的速度是".current(each($speed))."<br />";
echo "5挡的速度是".current(each($speed))."<br />";
echo "使用each函数实现数组指针的移动,进行数组遍历 <br />";
reset($speed);//这里是将数组指针指向数组首
while(list($key,$value)=each($speed)){
echo $key."=>".$value."<br />";
}
?>

Run results: Each implements pointer movement down The speed in 0 gear is 0 The speed in 1st gear is 40 The speed in 2nd gear is 80 The speed in 3rd gear is 120 The speed in 4th gear is 160 The speed in 5th gear is 200 Use each function to move the array pointer and traverse the array 0=>0 1=>40 2=>80 3=>120 4=>160 5=>200



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