PHP Array loop operation details the attached instance Code _php tutorial

Source: Internet
Author: User
PHP array is still more commonly used, so I studied the PHP array loop operation, here to take out and share with you, hope to be useful to everyone. PHP is basically an array language. Often to do a large number of PHP array loop operation, there are two main ways, one is foreach, the other is while, in the end what kind of good bad has been debated, although I am very early aware of this problem, but has not scrutiny, ignorant feeling has continued until now, In order to save a bit of CPU time later, summarize the following:

In the loop is the array "read" operation, then the foreach is faster than while the PHP array looping operation unformatted view copied to the Clipboard print code?
Copy CodeThe code is as follows:
foreach ($arrayas $value) {
Echo$value;
}
while (list ($key) =each ($array)) {
echo$array[$key];
}
foreach ($arrayas $value) {
Echo$value;
}
while (list ($key) =each ($array)) {
echo$array[$key];
}

In the loop is an array of "write" operations, while the while is faster than foreach:

No format view Copy to clipboard print code?
Copy CodeThe code is as follows:
foreach ($arrayas $key=> $value) {
echo$array[$key]= $value. ' ...';
}
while (list ($key) =each ($array)) {
$array [$key]= $array [$key]. ' ...';
}
foreach ($arrayas $key=> $value) {
echo$array[$key]= $value. ' ...';
}
while (list ($key) =each ($array)) {
$array [$key]= $array [$key]. ' ...';
}

Summary: It is generally assumed that foreach involves value copying, which is bound to be slower than while, but in fact, if you are simply reading the array in a loop, then foreach is very fast, because PHP uses a copy mechanism that is "copy-on-write, copy-in-writing", so it appears that The efficient read operation of foreach is not difficult to understand. In addition, since foreach is not suitable for handling array writes, we can draw a conclusion that in most cases code similar to foreach ($arrayas $key=> $value) should be replaced by the while (list ($key) =each ( $array)).

The speed differences generated by these techniques may not be obvious in small projects, but in large projects such as frameworks, a single request can easily involve hundreds of thousands of tens of thousands of array loops, and the difference will be magnified significantly.

For a small example of PHP arrays and loops, including two-dimensional arrays, Yang Hui triangles, get parameters, and rectangle diagonal summation, a friend is recommended to see

Copy CodeThe code is as follows:
1, using the loop statement, output any two-dimensional array.
$arr =array (
Array (1,2,3,4),
Array (5,6,7,8),
Array (9,10,11,12),
Array (13,14,15,16)
);
foreach ($arr as $var) {
foreach ($var as $val 1) {
echo "$val 1";
}
echo "
";
}
echo "
";
2, using the loop control statement, output Yang Hui triangle.
function Yanghuisanjiao ($line) {
$SC [][]=array ();
$SC [0][0]=1;
for ($i =1; $i <= $line; $i + +) {
for ($j =0; $j <= $i; $j + +) {
if ($j ==0 or $i = = $j) {
$SC [$i] [$j]=1; Set the first and last digit of each line to 1
}else{
$SC [$i] [$j]= $sc [$i -1][$j -1]+ $sc [$i -1][$j];
}
}
}
foreach ($sc as $value) {
foreach ($value as $v 1) {
echo $v 1. ' ';
}
Echo '

';
}
}
Yanghuisanjiao (5);
echo "
";
3. Use loops and pre-defined variables to get multiple parameters. The number of arguments is undefined.
function avg () {
$ags =func_get_args ();
$sum = 0;
foreach ($ags as $v) {
$sum + = $v;
}
Return ' average is: '. $sum/func_num_args ();
}
echo avg (1,2,3,4,5,6,7);

4, using the loop output a two-dimensional array, and the rectangle to find the diagonal elements of the and.
function Getsum ($theCount) {
$b = 0;
Echo '

';
echo "
















"For ($i =1; $i <= $theCount; $i + +) {echo" for ($j =1; $j <= $theCount; $j + +) {if ($j = = $i | | $theCount +1-$i = = $j) {echo " "; $b = $b + $j; if ($j = = $i && $theCount +1-$i = = $j) {$b = $b + $j;}} else{echo " "; }} echo " ";} echo "
$j$j





http://www.bkjia.com/PHPjc/326468.html www.bkjia.com true http://www.bkjia.com/PHPjc/326468.html techarticle PHP Array is still more commonly used, so I studied the PHP array loop operation, here to take out and share with you, hope to be useful to everyone. PHP is basically an array language ...

  • The sum of the "; echo" diagonal Elements is: ". $b;} Getsum (6);? >

    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.