PHP array introductory tutorial: array pointer operations

WBOY
Release: 2016-07-25 08:57:51
Original
1034 people have browsed it
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
?>
Copy after login

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 />";
}
?>
Copy after login

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



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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!