Home > php教程 > php手册 > body text

php中使用key,value,current,next和prev函数遍历数组的方法

PHP中文网
Release: 2017-06-06 13:39:35
Original
1744 people have browsed it

这篇文章主要介绍了php中使用key,value,current,next和prev函数遍历数组的方法,较为详细的分析了php中数组遍历的常用技巧与实例用法,需要的朋友可以参考下

本文实例讲述了php中使用key,value,current,next和prev函数遍历数组的方法。分享给大家供大家参考。具体分析如下:

php中针对数组遍历有一系列的函数使我们可以非常方便的操作数组,要遍历一个数组,第一步就是要将指针指向数组开头,使用reset()函数。

使用prev()和next()函数可以查看数组的上一个和下一个元素。在然和位置都可以使用current()函数获得当前的值,使用key()函数获得键值

$array = array('foo' => 'bar', 'baz', 'bat' => 2);
function displayArray(&$array) {
reset($array);
while (key($array) !== null) {
echo key($array) .": " .current($array) . PHP_EOL;
next($array);
}
}
Copy after login

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
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!