Heim  >  Artikel  >  Backend-Entwicklung  >  php中使用key,value,current,next和prev函数遍历数组的方法_PHP

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

WBOY
WBOYOriginal
2016-06-01 11:06:15913Durchsuche

本文实例讲述了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);
}
}

希望本文所述对大家的php程序设计有所帮助。

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