問題描述
我有一個(gè)數(shù)據(jù)庫調(diào)用,我想弄清楚 $key =>$value
在 foreach
循環(huán)中執(zhí)行.
I have a database call and I'm trying to figure out what the $key => $value
does in a foreach
loop.
我問的原因是因?yàn)檫@兩個(gè)代碼輸出相同的東西,所以我試圖理解為什么它是這樣寫的.代碼如下:
The reason I ask is because both these codes output the same thing, so I'm trying to understand why it's written this way. Here's the code:
1)在foreach中使用$key =>$value
1)In foreach use $key => $value
foreach($featured as $key => $value){
echo $value['name'];
}
這個(gè)輸出與:
2)在 foreach 中只使用 $value
2)In foreach use only $value
foreach($featured as $value) {
echo $value['name'];
}
所以我的問題是,$key => 和有什么區(qū)別?$value
或 $value
在 foreach
循環(huán)中.如果這有所不同,數(shù)組是多維的,我只想知道為什么在 foreach
循環(huán)中將 $key
傳遞給 $value
.
So my question is, what is the difference between $key => $value
or just $value
in the foreach
loop. The array is multidimensional if that makes a difference, I just want to know why to pass $key
to $value
in the foreach
loop.
推薦答案
好吧,$key =>foreach 循環(huán)中的 $value
指的是關(guān)聯(lián)數(shù)組中的鍵值對(duì),其中鍵作為索引來確定值,而不是像 0,1,2,... 這樣的數(shù)字在 PHP 中,關(guān)聯(lián)數(shù)組看起來像這樣:
Well, the $key => $value
in the foreach loop refers to the key-value pairs in associative arrays, where the key serves as the index to determine the value instead of a number like 0,1,2,... In PHP, associative arrays look like this:
$featured = array('key1' => 'value1', 'key2' => 'value2', etc.);
在 PHP 代碼中:$featured
是循環(huán)遍歷的關(guān)聯(lián)數(shù)組,as $key =>$value
表示每次循環(huán)運(yùn)行并從數(shù)組中選擇一個(gè)鍵值對(duì)時(shí),它將鍵存儲(chǔ)在本地 $key
變量中以在循環(huán)塊內(nèi)使用和值在本地 $value
變量中.因此,對(duì)于我們上面的示例數(shù)組,foreach 循環(huán)將到達(dá)第一個(gè)鍵值對(duì),如果您將 指定為 $key =>;$value
,它會(huì)將 'key1'
存儲(chǔ)在 $key
變量中,將 'value1'
存儲(chǔ)在 $value 中
變量.
In the PHP code: $featured
is the associative array being looped through, and as $key => $value
means that each time the loop runs and selects a key-value pair from the array, it stores the key in the local $key
variable to use inside the loop block and the value in the local $value
variable. So for our example array above, the foreach loop would reach the first key-value pair, and if you specified as $key => $value
, it would store 'key1'
in the $key
variable and 'value1'
in the $value
variable.
由于您不在循環(huán)塊中使用 $key
變量,因此添加或刪除它不會(huì)改變循環(huán)的輸出,但最好包含鍵值對(duì)表明它是一個(gè)關(guān)聯(lián)數(shù)組.
Since you don't use the $key
variable inside your loop block, adding it or removing it doesn't change the output of the loop, but it's best to include the key-value pair to show that it's an associative array.
還要注意 as $key =>$value
指定是任意的.您可以將其替換為 as $foo =>$bar
并且只要您將循環(huán)塊內(nèi)的變量引用更改為新變量 $foo
和 $bar
,它就可以正常工作.但是讓它們成為 $key
和 $value
有助于跟蹤它們的含義.
Also note that the as $key => $value
designation is arbitrary. You could replace that with as $foo => $bar
and it would work fine as long as you changed the variable references inside the loop block to the new variables, $foo
and $bar
. But making them $key
and $value
helps to keep track of what they mean.
這篇關(guān)于"as $key => 之間的區(qū)別$值"和“作為 $value"在 PHP foreach 中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!