Php array loop output implementation method _ PHP Tutorial

Source: Internet
Author: User
Tags php foreach
Php array loop output implementation method. 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 use four loop statements commonly used in php to implement single array or multi-dimensional array loop output.

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 code is as follows:

'Bacon and eggs ', 'lunch' => 'Roast beef', 'Dinner '=> 'lasaga ');

/* Returns the array ('Breakfast ', 'lunch', 'dinner') with numeric indices */

$ Result = array_keys ($ menu); print_r ($ result); print "";

/*
Returns the array ('bacon and eggs ', 'Roast beef', 'lasagna ') with numeric indices */

$ Result = array_values ($ menu); print_r ($ result );

?>

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 code is as follows:
List = array ('upid '=> '1', 'title' => 'company News', 'list' => array ('id' => '3 ', 'title' => 'company news title test! ', 'Intime' => '2014-29-5 '));
Foreach ($ list as $ v => $)
{
Echo $ a ['upid '].'-'. $ a ['title'];
Foreach ($ a ['list'] as $ B ){
Echo $ B ['title'];
}

}

Use the array key value

The code is as follows:
$ Arr_age = array ("wang" => 18, "li" => 20, "zhang" => 25 );
Foreach ($ arr_age as $ key => $ age ){
Echo $ key, ':', $ age ,'
';
}
?>

Running example output:

Wang: 18
Li: 20
Zhang: 25

When foreach starts to execute, the pointer inside the array automatically points to the first unit, which means that the reset () call is not required 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 code is as follows:


$ Shuzu = array ("ni", "wo", "ta", "php", "mysql ");
$ Count = count ($ shuzu );
Echo "use for to traverse arrays ";
Echo"
$ Nbsp;
";
For ($ I = 0; $ I <$ count; $ I ++)
{
$ J = $ I + 1;
Echo "the {$ j} element is: $ shuzu [$ I]";
Echo"
$ Nbsp;
";
}
?>

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 that, like array (), list () is actually a language structure, not a function.

Syntax
List (var1, var2. ..) parameter description
Var1 is required. The first variable to be assigned a value.
Var2 is optional. There can be 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 code is as follows:

$ Colors = array ('red', 'blue', 'green', 'yellow ');

While (list ($ key, $ val) = each ($ colors )){
Echo "other list of $ val.
";
}


For more details, see http://www.bKjia. c0m/phper/php/36112.htm

Bytes. In...

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.