I recently bought PHP and MySQL web development and saw the array loop statements. It is worth your attention and I will keep some handwriting for later reading.
Generally, foreach uses more
<? PHP
$ Price = array ('apple' => 10, 'Orange '=> 20, 'banner' => 30 );
Foreach ($ price as $ key => $ value)
{
Echo $ key. '=>'. $ value. '<br> ';
}
Echo '<br> ';
?>
There is also a more advanced and common method
<? PHP
$ Shuiguo = array ('apple' => 10, 'Orange '=> 20, 'banner' => 30 );
While (List ($ changpin, $ jiage) = each ($ shuiguo ))
{
Echo "$ changpin => $ jiage". '<br> ';
}
?>
I haven't paid much attention to it before. Today I am doing it myself. It's good to know about new things, and I am still doing it myself.
The List () function can be used to break down an array into a series of values and name new variables. Click here if you do not understand list
Two sectionsCodeThe output is the same.
Note that when using the each () function, the array records the current element. If you want to use the array twice in the same script. You need to use reset () to drop the current element and reset it to the beginning of the array.
<? PHP
$ Price = array ('apple' => 10, 'Orange '=> 20, 'banner' => 30 );
Foreach ($ price as $ key => $ value)
{
Echo $ key. '=>'. $ value. '<br> ';
}
Echo '<br> ';
Reset ($ price );
While (List ($ key, $ value) = each ($ price ))
{
Echo "$ key => $ value", "<br> ";
}
?>
In this way, you can still use the array $ price.
I have some in the book. As a newbie, I have to do it myself. I have to think about the effect, understand it, write a post, and forget to look at it later. It's superficial, and the language is poor.