foreach中的$ k => $ v($ ex为$ k => $ v)是什么意思?

可能重复:
“=>”在PHP中意味着什么?

$k => $v是什么意思?


这意味着对于遍历变量$ex中的每个键值对,键被分配到$k并且值为$v 。 换一种说法:

$ex = array("1" => "one","2" => "two", "3" => "three");
foreach($ex as $k=>$v) {
   echo "$k : $v n";
}

输出:

1 : one
2 : two
3 : three

你正在循环一个数组。 数组有键(数字,或者当你有一个关联数组时可以是字符串)以及'属于'这些键的值。

你的$k是关键, $v是价值,你用foreach循环每个单独的对。


$k$v值存储在数组中的索引号。 $k可以是数组的关联索引:

$array['name'] = 'shakti';
$array['age'] = '24';

foreach ($array as $k=>$v)
{
    $k points to the 'name' on first iteration and in the second iteration it points to age.
    $v points to 'shakti' on first iteration and in the second iteration it will be 24.
}
链接地址: http://www.djcxy.com/p/1771.html

上一篇: What does $k => $v in foreach($ex as $k=>$v) mean?

下一篇: Use of => in PHP