PHP for Loop array

Source: Internet
Author: User

/*
For syntax

The code is as follows Copy Code
for (EXPR1; expr2; expr3) {
Statement
}

See the For instance tutorial below

*/

The code is as follows Copy Code
$for = Array (1,2,3,4,5,6);
$forFor = Array (
Array (1),
Array (2),
Array (3)
);


Let's take a look at an instance of a set of data with a for loop

The code is as follows Copy Code

$arrayLen = sizeof ($for);

for ($i = 0; $i < $arrayLen; $i + +)
{
echo $for [$i], ' <br> ';
}


/*
Results
1
2
3
4
5
6
This is the result we want.

For to process a two-dimensional array

The code is as follows Copy Code
*/
for ($j =0; $j < $arrayLen; $j + +)
{
Echo ($forFor ($j));
}

Fatal error:function name must being a string in hint appears

Let's switch to a different user with a foreach instance.

The code is as follows Copy Code
foreach ($forFor as $v => $vv)
{
Print_r ($VV);
}

/*
Output
(
[0] => 1
)
Array
(
[0] => 2
)
Array
(
[0] => 3
)
Exactly the data we want,

Summarize
Each statement has its own purpose, and we want to see how it is more reasonable to use functions that are appropriate for your current operation to do your work quickly.
From the above, for the one dimensional data, and a single loop, foreach can manipulate one-dimensional data in two-dimensional data.

This site original article reproduced annotated from www.111cn.net/phper/php.html

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.