2011-01-21 77 views

回答

13

這意味着,在橫越的可變$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 
3

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

你遍歷數組。數組有鍵(數字,或者當你有一個關聯數組時可以是字符串)以及'屬於'這些鍵的值。

您的$k是關鍵,$v是價值,你正在循環每個單獨的對與foreach。