Generally, foreach is used more
The code is as follows:
<?php
$price=array(‘apple’=>10,’orange’ =>20,’banner’=>30);
foreach($price as $key=>$value)
{
echo $key.’=>’.$value.’
‘;
}
echo ‘
‘;
?>
There is a more advanced and common method
The code is as follows:
<?php
$shuiguo=array(‘apple’=>10,’orange’=>20,’banner’=>30);
while( list($changpin,$jiaage)=each($shuiguo))
{
echo “$changpin=>$jiaage”.’
‘;
}
?>
I didn’t really pay attention to it before, but today I did it myself, it’s not bad, and I’ve learned something new, but I’m still too good at it, hey
The list() function can be used to decompose an array into a series A value of , allowing the new variable to be named. If you don’t understand list, click here
The output of the two codes is the same.
It should be noted that when the each() function is used, the array will record the current element. If you want to use the array twice in the same script. You need to use reset() to reset the current element to the beginning of the array.
The code is as follows:
<?php
$price=array(‘apple’=>10,’orange’=>20,’banner ‘=>30);
foreach($price as $key=>$value)
{
echo $key.’=>’.$value.’
‘;
}
echo ‘
‘;
reset($price);
while(list($key,$value)=each($price))
{
echo “$key =>$value”,”
“;
}
?>
In this way, the array $price can still be used.
It’s in the book, as a novice, I do it myself, knock down to see the effect, understand, write a post, so that I forget to read it later, the words are superficial, the language expression is not good, laugh up.
The code is as follows:
<?php
/*
*Loop statement learning notes in PHP
*1 .while loop
if(expression)
Only execute one statement at a time.
while(expression){
Repeatedly execute this loop body;
}
*2.do-while loop
*3.for loop
*There are two different loop conditions Two types of loops
*one: counting loop for
*the other: conditional loop while do-while //foreach
*several statements related to loops
*break;/ / can be used for flow control and loop body, jumping out of the loop.
continue;//Can only be used in the loop body, exit this loop. exit;
return;
*The writing cycle should not exceed three layers.
* The flow control statement of the loop should not exceed five levels as much as possible.
*/
$num=0;
while($num<100){
echo “This is the result of {$num} output
“;
$ num++;
}
//
echo ‘<table border="1"
align=”center”>’;
echo ‘
Use a while loop to output
The table
‘;
$i=0;
while($i<1000){
if($i%10==0){
if($i%20==0){
$bg=”#ffffff”;
}else{
$bg=”#cccccc”;
}
echo ‘ <tr
Onmouseover=”lrow(this)” Onmouseout=”drow
(this)” bgColor=”‘.$bg.'”>’;
}
echo ‘
‘;
$i++;
if($i%10==0){
echo ‘
‘;
}
}
echo ‘
‘;
//
$i=0;
do{
echo “$i :this is do*while
“;
$i++;
}while($i<10);
//
for(initialization condition; conditional expression; increment){
loop body;
}
/*
The do-while loop is to execute the code once, and then judge, while the
while loop is to judge first, if it is true, it will continue to loop, if
false, it will not loop.
*/
//99 multiplication table
for($i=1; $i<=9; $i++){
for($j=1; $j<=$i ; $j++){
echo “$j x $i =
“.$j*$i.” “;
}
echo ‘
‘;