Php $k=>$foreach中的v($exas$k=>;$v)是什么意思?

Php $k=>$foreach中的v($exas$k=>;$v)是什么意思?,php,foreach,Php,Foreach,可能的重复项: $k=>$v是什么意思?$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'

可能的重复项:


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

$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.
}

这意味着对于可遍历变量
$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在每一对中循环。

“我不是php专家”这就是手册存在的原因…有东西阻止你运行代码?(相关)完全重复:谢谢shakti,解释得很好