网站首页 > 知识剖析 正文
array each ( array &$array )
返回 array 数组中当前指针位置的键/值对并向前移动数组指针。键值对被返回为四个单元的数组,键名为 0, 1, key 和 value。
单元 0 和 key 包含有数组单元的键名,1 和 value 包含有数据。
如果内部指针越过了数组的末端, 则 each() 返回 FALSE。
<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>
Array
{
[1] => bob
[value] => bob
[0] => 0
[key] => 0
}
注意: 【PHP】php7.2报错The each() function is deprecated. This message will be suppressed on furthe
php7.2以上 废除了 each()方法, 项目中用到的地方会出现以下报错
The each() function is deprecated. This message will be suppressed on further calls
解决办法。很简单
while (list($key, $val) = each($array)) {
#code
}
改为
foreach ($array as $key => $val) {
#code
}
list — 把数组中的值赋给一些变量
void list ( mixed $varname , mixed $... ) 仅能用于数字索引的数组并假定数字索引从 0 开始。
a. list()=array(); 需要将一个数组赋值给这个函数
b. 数组中的元素个数, 要和list()函数中的参数个数相同
c. 数组中的每个元素值会赋值list()函数中的每个参数, list()将每个参数转为变量
d. list()只能接收索引数组
e. 按索引的下标的顺序
实例1:
<?php
$arr = array(1, 'ab' => 2, 3 , 8=> 18, 2=>20 );
//注意上述数组的下标是0, 'ab', 1, 8, 2
list($v1, $v2, $v3 ) = $arr; //这里不是赋值语句,而是被称为"语法结构"
//以上一行相当于
//$v1 = $arr[0]; $v2=$arr[1];$v3=$arr[2];
echo("<br />v1=$v1 "); //v1=1
echo "<br />v2=$v2 "; //v2=3
echo "<br />v3=$v3 "; //v3=20
?>
实例2:
<?php
$info = array('coffee','brown','caffeine');
list($drink,$color,$power) = $info;
echo "$drink is $color and $power makes it special.\n";
list($drink,,$power) = $info;
echo "$drink has $power.\n";
list(,$drink) = $info;
echo "I need $drink";
?>
输出结果:coffee is brown and caffeine makes it special. coffee has caffeine. I need brown
实例3:
<?php
$user=array("id"=>1, "name"=>"zhangsan", "age"=>10, "sex"=>"nan");
while(list($key, $value)=each($user)){
echo $key."==>".$value."<br>";
}
?>
输出结果:id==>1
name==>zhangsan
age==>10
sex==>nan
猜你喜欢
- 2024-12-24 [32](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 [18](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 [16](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 [19](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 [22](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 [24](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 [17](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 [13](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 [15](外代二线)巴黎时装周——Each X Other品牌时装秀
- 2024-12-24 for的用法 to的用法
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)