Home > Backend Development > PHP Tutorial > Examples of foreach and list loop statements in php_PHP tutorial

Examples of foreach and list loop statements in php_PHP tutorial

WBOY
Release: 2016-07-13 17:13:00
Original
848 people have browsed it

The article focuses on the usage of foreach loop and list loop statements. Well, let’s take a look at the implementation. I won’t go into too much detail. Students in need can refer to it.

I recently bought a PHP and MySQL web development program and saw array loop statements. It’s worth paying attention to, and hereby leave some writing for future reading.

Generally foreach is used more

The code is as follows
 代码如下 复制代码

$price=array('apple'=>10,'orange'=>20,'banner'=>30);
    foreach($price as $key=>$value)
    {
        echo $key.'=>'.$value.'
';
    }
    echo '
';
?>

Copy code

 代码如下 复制代码

$shuiguo=array('apple'=>10,'orange'=>20,'banner'=>30);
   
    while(list($changpin,$jiage)=each($shuiguo))
    {
        echo "$changpin=>$jiage".'
';
    }
?>

$price=array('apple'=>10,'orange'=>20,'banner'=>30); foreach($price as $key=>$value)

{
echo $key.'=>'.$value.'
';

}

echo '
';

?>

 代码如下 复制代码

$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","
";
    }
?>

There is a more advanced and common method
The code is as follows
Copy code

$shuiguo=array('apple'=>10,'orange'=>20,'banner'=>30); While(list($changpin,$jiage)=each($shuiguo)) { echo "$changpin=>$jiage".'
'; } ?> I really didn’t pay much attention to it before, but I did it myself today, which was pretty good. I learned something new again, but I’m still too good at it, eh The list() function can be used to decompose an array into a series of values, allowing new variables to be named. Those who don’t understand lists

http://www.bKjia.c0m/phper/24/032a7c95555c423729b071aef4afd3c4.htm
The output of the two pieces of code is the same.
It should be noted that when using the each() function, 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 Copy code
$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","
"; } ?> This way you can still use the array $price. http://www.bkjia.com/PHPjc/629245.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629245.htmlTechArticleThe article focuses on the usage of foreach loop and list loop statements. Well, let’s take a look at the implementation. I won’t go into too much detail. Students in need can refer to it. Recently bought...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template