Php array loop output implementation method

Source: Internet
Author: User
In the past, we often thought about how to implement php array loop output. This article describes how to use four loop statements commonly used in php to implement single array or multi-dimensional array loop output.

In the past, we often thought about how to implement php array loop output. This article describes how to implement single-array or multi-dimensional array loop output using four loop statements commonly used in php.

In PHP, we can use the following loop statement:

While

The code block is executed cyclically as long as the specified condition is true.

Do... while

Execute a code block first, and then repeat the loop when the specified condition is set.

For

Number of times the code block is executed cyclically

Foreach

Loop code blocks based on each element in the array

First, we use the php built-in function to traverse the php array.

The array_keys () and array_values () functions are available to obtain a list of all the keywords and corresponding values in the array.

The instance code is as follows:

  1. 'Bacon and eggs ', 'lunch' => 'Roast beef', 'Dinner '=> 'lasaga ');
  2. /* Returns the array ('Breakfast ', 'lunch', 'dinner') with numeric indices */
  3. $ Result = array_keys ($ menu); print_r ($ result); print "";
  4. /*
  5. Returns the array ('bacon and eggs ', 'Roast beef', 'lasagna ') with numeric indices */
  6. $ Result = array_values ($ menu); print_r ($ result );
  7. ?>

Use foreach to traverse php arrays

The PHP foreach () syntax structure is used to traverse operations or output arrays. foreach () can only be used to traverse arrays or objects, an error occurs when you try to use it for another data type or an uninitialized variable.

The instance code is as follows:

  1. List = array ('upid '=> '1', 'title' => 'company News', 'list' => array ('id' => '3 ', 'title' => 'company news title test! ', 'Intime' => '2014-29-5 '));
  2. Foreach ($ list as $ v => $)
  3. {
  4. Echo $ a ['upid '].'-'. $ a ['title'];
  5. Foreach ($ a ['list'] as $ B ){
  6. Echo $ B ['title'];
  7. }
  8. }

The code for using the array key value instance is as follows:

  1. $ Arr_age = array ("wang" => 18, "li" => 20, "zhang" => 25 );
  2. Foreach ($ arr_age as $ key => $ age ){
  3. Echo $ key, ':', $ age ,' ';
  4. }
  5. ?>

Running example output:

Wang: 18

Li: 20

Zhang: 25

When foreach starts execution, the pointer inside the array automatically points to the first unit, which means that the reset () is not called before the foreach loop ().

For () loop traversal array

If you are operating on an array of continuous key values, you can also use the for () loop to traverse the array:

The instance code is as follows:

  1.  
  2. $ Shuzu = array ("ni", "wo", "ta", "php", "mysql ");
  3. $ Count = count ($ shuzu );
  4. Echo "use for to traverse arrays ";
  5. Echo"
    $ Nbsp;
    ";
  6. For ($ I = 0; $ I <$ count; $ I ++)
  7. {
  8. $ J = $ I + 1;
  9. Echo "the {$ j} element is: $ shuzu [$ I]";
  10. Echo"
    $ Nbsp;
    ";
  11. }
  12. ?>
  13.  

25

You can also use list () and each () to traverse the php array, but the test shows that the efficiency is not as high as that of foreach ().

List function

The list () function assigns values to a group of variables with elements in the array.

Note: Like array (), list () is actually a language structure, not a function.

Syntax

List (var1, var2. ..) parameter description

Var1 required. The first variable to be assigned a value.

Var2 is optional. you can have multiple variables.

Tips and comments

Note: This function is only used for numeric index arrays. it is assumed that the numeric index starts from 0.

*/

The instance code is as follows:

  1. $ Colors = array ('red', 'blue', 'green', 'yellow ');
  2. While (list ($ key, $ val) = each ($ colors )){
  3. Echo "other list of $ val. ";
  4. }

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.