each() Function
each()函数是PHP中的内建函数,它返回一个数组,数组中有4个元素,元素值为2个元素(1和值),元素键为2个元素(0和键),并将光标向前移动。
语法
1 | each(array) |
参数
1 2 3 | Array: It specifies the array which is being taken as input and used for each() function. |
返回值
1 2 3 | 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. |
程序1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?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)); ?> |
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Array ( [1] => Ram [value] => Ram [0] => 0 [key] => 0 ) Array ( [1] => Shita [value] => Shita [0] => 1 [key] => 1 ) |
程序2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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)); ?> |
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Array ( [1] => Ram [value] => Ram [0] => 101 [key] => 101 ) Array ( [1] => Geeta [value] => Geeta [0] => 105 [key] => 105 ) |
参数