Example of foreach and list loop statements in php

Source: Internet
Author: User
Tags php and php and mysql

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

The code is as follows: Copy code

<? 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

The code is as follows: Copy code

<? 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. List-insensitive
Http://www.111cn.net/phper/24/032a7c95555c423729b071aef4afd3c4.htm

The two pieces of code output are 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.

The code is as follows: Copy code

<? 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.