首页  >  文章  >  php教程  >  PHP each()与list()函数

PHP each()与list()函数

WBOY
WBOY原创
2016-06-08 17:24:341351浏览

文章总结一下关于PHP each()与list()函数 有需要的朋友可参考一下。



void list ( mixed varname, mixed … )

注: list() 仅能用于数字索引的数组并假定数字索引从 0 开始

例子
 代码如下 复制代码


$info = array('coffee', 'brown', 'caffeine');

// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.n";

// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.n";

// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!n";

// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>

 代码如下

复制代码


代码如下 复制代码

$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($people));
?>

$info = array('coffee', 'brown', 'caffeine');

// Listing all the variables

list($drink, $color, $power) = $info;

echo "$drink is $color and $power makes it special.n";
 代码如下 复制代码

$cities=array("California"=>array("Martinez","San Francisco","Los Angeles"),
              "New York"=>array("New York","Buffalo")
            );
while (list($key,$value)=each($cities))
{    //echo $key;
//echo "fdash";
//echo
while (list($key0,$val)=each($value)){
echo "elements:$key0,value:$val
n";
}
}
?>

// Listing some of them list($drink, , $power) = $info; echo "$drink has $power.n";<script>ec(2);</script> // Or let's skip to only the third one list( , , $power) = $info; echo "I need $power!n"; // list() doesn't work with strings list($bar) = "abcde"; var_dump($bar); // NULL ?> each() 函数生成一个由数组当前内部指针所指向的元素的键名和键值组成的数组,并把内部指针向前移动。 array each ( array &array ) 返回 array 数组中当前指针位置的键/值对并向前移动数组指针。键值对被返回为四个单元的数组,键名为 0,1,key 和 value。单元 0 和 key 包含有数组单元的键名,1 和 value 包含有数据。
 代码如下 复制代码
$people = array("Peter", "Joe", "Glenn", "Cleveland");<🎜> print_r (each($people));<🎜> ?> each() 经常和 list() 结合使用来遍历数组, 例如:
 代码如下 复制代码
$cities=array("California"=>array("Martinez","San Francisco","Los Angeles"),               "New York"=>array("New York","Buffalo")             ); while (list($key,$value)=each($cities)) {    //echo $key; //echo "fdash"; //echo while (list($key0,$val)=each($value)){ echo "elements:$key0,value:$val
n"; } } ?>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn