您好,登錄后才能下訂單哦!
如何使用php中each方法?
each定義和用法
each() 函數(shù)返回當(dāng)前元素的鍵名和鍵值,并將內(nèi)部指針向后移動。
該元素的鍵名和鍵值返回到帶有四個元素的數(shù)組中。兩個元素(1 和 Value)包含鍵值,兩個元素(0 和 Key)包含鍵名。
相關(guān)的方法:
current() - 返回數(shù)組中的當(dāng)前元素的值。 end() - 將內(nèi)部指針指向數(shù)組中的最后一個元素,并輸出。 next() - 將內(nèi)部指針指向數(shù)組中的下一個元素,并輸出。 prev() - 將內(nèi)部指針指向數(shù)組中的上一個元素,并輸出。 reset() - 將內(nèi)部指針指向數(shù)組中的第一個元素,并輸出。
提示:each() 函數(shù)在 PHP 7.2.0 中被棄用了。
語法
each(array)
參數(shù)
array 必需。規(guī)定要使用的數(shù)組。
返回值: 返回當(dāng)前元素的鍵名和鍵值。該元素的鍵名和鍵值返回到帶有四個元素的數(shù)組中。兩個元素(1 和 Value)包含鍵值,兩個元素(0 和 Key)包含鍵名。如果沒有更多的數(shù)組元素,則函數(shù)返回 FALSE。
實例 1
與頁面頂部的實例相同,但是本例通過循環(huán)輸出整個數(shù)組:
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); reset($people); while (list($key, $val) = each($people)) { echo "$key => $val<br>"; } ?>
運行結(jié)果:
0 => Peter 1 => Joe 2 => Glenn 3 => Cleveland
實例 2
所有相關(guān)方法的演示:
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); echo current($people) . "<br>"; // The current element is Peter echo next($people) . "<br>"; // The next element of Peter is Joe echo current($people) . "<br>"; // Now the current element is Joe echo prev($people) . "<br>"; // The previous element of Joe is Peter echo end($people) . "<br>"; // The last element is Cleveland echo prev($people) . "<br>"; // The previous element of Cleveland is Glenn echo current($people) . "<br>"; // Now the current element is Glenn echo reset($people) . "<br>"; // Moves the internal pointer to the first element of the array, which is Peter echo next($people) . "<br>"; // The next element of Peter is Joe print_r (each($people)); // Returns the key and value of the current element (now Joe), and moves the internal pointer forward ?>
運行結(jié)果:
Peter Joe Joe Peter Cleveland Glenn Glenn Peter Joe Array ( [1] => Joe [value] => Joe [0] => 1 [key] => 1 )
以上就是如何使用php中each方法的詳細(xì)內(nèi)容,更多請關(guān)注億速云其它相關(guān)文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。