each()函數是PHP中的內置函數,該函數本質上返回一個包含四個元素的數組,兩個元素(1和Value)作為元素值,兩個元素(0和Key)作為元素鍵,並將光標向前移動。
用法:
each(array)
參數:
Array: It specifies the array which is being taken as input and used for each() function.
返回值:
It returns the current element key and value which are an array with four elements out of which two elements (1 and Value) for the element value, and two elements (0 and Key) for the element key.It returns FALSE if there are no array elements.
PHP版本:
4+
讓我們看一下PHP程序:
示例1:
<?php
// PHP program to demonstrate working of each()
// for simple array.
// input array contain some elements inside.
$a = array("Ram", "Shita", "Geeta");
// each() function return in the array with four elements
// Two elements (1 and Value) for the element value which
// are Ram, and two elements (0 and Key) for the element
// key which are not given here so output is zero.
print_r (each($a));
// Next set is printed as cursor is moved
print_r (each($a));
?>
輸出:
Array ( [1] => Ram [value] => Ram [0] => 0 [key] => 0 ) Array ( [1] => Shita [value] => Shita [0] => 1 [key] => 1 )
示例2:
<?php
// PHP program to demonstrate working of each()
// for associative array.
$a = array("101"=>"Ram", "105"=>"Geeta", "104"=>"Geek");
// each() function return in the array with four elements
// Two elements (1 and Value) for the element value which
// are Ram, and two elements (0 and Key) for the element
// key which are Boy.
print_r (each($a));
// Next set is printed as cursor is moved
print_r (each($a));
?>
輸出:
Array ( [1] => Ram [value] => Ram [0] => 101 [key] => 101 ) Array ( [1] => Geeta [value] => Geeta [0] => 105 [key] => 105 )
參考:
http://php.net/manual/en/function.each.php
相關用法
- p5.js day()用法及代碼示例
- PHP dir()用法及代碼示例
- PHP each()用法及代碼示例
- p5.js second()用法及代碼示例
- p5.js int()用法及代碼示例
- d3.js d3.max()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- p5.js str()用法及代碼示例
- p5.js arc()用法及代碼示例
- d3.js d3.hcl()用法及代碼示例
- d3.js d3.lab()用法及代碼示例
- p5.js sq()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 PHP | each() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。