PHP Traversal Array Statement Summary (Foreach,for,list,each) _php Tutorial

Source: Internet
Author: User
foreach to access, is the order of traversal fixed? In what order does it traverse?

Like what:

The code is as follows

$colors = Array (' Red ', ' blue ', ' green ', ' yellow ');
foreach ($colors as $color) {
Add your Codes
}
?>

Example 2

$capitals = Array ("Ohio" = "Columbus", "towa" = "Des Moines", "Arizona" and "Phoenix");
foreach ($capitals as $key = = $val) {
Add your Codes
}


while ()

while () is usually used in conjunction with list (), each ().

#example2:

The code is as follows

$colors = Array (' Red ', ' blue ', ' green ', ' yellow ');

while (list ($key, $val) = each ($colors)) {
echo "Other list of $val.
";
}
?>

Show Results:

Other list of red.
Other list of blue.
Other list of green.
Other list of yellow.

3. for ()
#example3:

The code is as follows


$arr = Array ("0" = "Zero", "1" = "One", "2" = "one");

for ($i = 0; $i < count ($arr); $i + +) {
$str = $arr [$i];
echo "The number is $str.
";
}
?>
Show Results:

The number is zero.
The number is one.
The number is a.

========= The following is a description of the function ==========

Key ()
Mixed key (array Input_array)

The key () function returns the keys element at the current pointer position in Input_array.

#example4

The code is as follows


$capitals = Array ("Ohio" = "Columbus", "towa" = "Des Moines", "Arizona" and "Phoenix");
echo "

Can you name the capitals of these states?

";
while ($key = key ($capitals)) {
echo $key. "
";
Next ($capitals);
Each key () call does not push the pointer. To do this, use the next () function
}
?>

Can you name the capitals of these states?
Ohio
Towa
Arizona

each () function iterates over an array

Example 1

The code is as follows

$people = Array ("Peter", "Joe", "Glenn", "Cleveland");
Print_r (each ($people));
?>

Output:

Array ([1] = peter [value] = peter [0] = 0 [key] = 0)

Sub 2
Each () is often used in conjunction with list () to iterate through the array. This example is similar to the previous example, but the loop outputs the entire array:

The code is as follows

Copy Code

$people = Array ("Peter", "Joe", "Glenn", "Cleveland");

Reset ($people);

while (list ($key, $val) = each ($people))
{
echo "$key and $val
";
}
?>

Output:

0 = Peter
1 = Joe
2 = Glenn
3 = Cleveland


Recursive traversal of multidimensional arrays

The code is as follows

/*
* -------------------------------------------------
* Author:
* url:www.45it.com* date:2011-03-09
* -------------------------------------------------
*/
function Arr_foreach ($arr)
{
if (!is_array ($arr))
{
return false;
}

foreach ($arr as $key = $val)
{
if (Is_array ($val))
{
Arr_foreach ($val);
}
Else
{
echo $val. '
';
}
}
}

$arr 1 = Array (1=>array (11,12,13,14=>array (141,142)), 2,3,4,5);

Echo '

';
Print_r ($arr 1);
Echo '
 

Arr_foreach ($arr 1);
?

Result

Array
(
[1] = = Array
(
[0] +-one
[1] = [2] [+] [+] = [+] [] = Array
(
[0] = = 141
[1] = 142
)

)

[2] + 2
[3] + 3
[4] = 4
[5] = 5)
One


141
142
2
3
4
5

http://www.bkjia.com/phpjc/371384.html www.bkjia.com true http://www.bkjia.com/phpjc/371384.html techarticle foreach to access, the order of traversal is fixed? In what order does it traverse? For example: Code as follows? PHP $colors = Array (' Red ', ' blue ', ' green ', ' yellow '); foreach ($colors as $color) {//...

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