当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP each()用法及代码示例


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



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 PHP | each() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。