Heim  >  Artikel  >  php教程  >  php中使用key,value,current,next和prev函数遍历数组的方法

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

PHP中文网
PHP中文网Original
2017-06-06 13:39:351824Durchsuche

这篇文章主要介绍了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);
}
}

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn