开发者

What does $k => $v in foreach($ex as $k=>$v) mean? [duplicate]

开发者 https://www.devze.com 2023-02-06 12:47 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: What does “=>” mean 开发者_开发百科in PHP?
This question already has answers here: Closed 12 years ago.

Possible Duplicates:

What does “=>” mean 开发者_开发百科in PHP?

What does $k => $v mean?


It means that for every key-value pair in the traversable variable $ex, the key gets assigned to $k and value to $v. In other words:

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

outputs:

1 : one
2 : two
3 : three


$k is the index number where the $v value is stored in an array. $k can be the associative index of an array:

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


You're looping over an array. Arrays have keys (numbers, or could be strings when you have an associative array) and values that 'belong' to those keys.

Your $k is the key, the $v is the value, and you're looping trough each separate pair with a foreach.

0

精彩评论

暂无评论...
验证码 换一张
取 消