PHP Array Loop operation Detailed introduction of the example code _php tips

Source: Internet
Author: User

PHP array is more commonly used, so I studied the PHP array loop operation, here to share with you, I hope to be useful. 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 what kind of bad has been arguing, although I have long been aware of this problem, but has been no scrutiny, ignorant of the feeling has been continued until now, In order to save some CPU time later, the following summary:

In the loop is an array "read" operation, then foreach is faster than while, PHP array loop operation unformatted view Copy to clipboard print code?

Copy Code code 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 the array write operation, while the while is faster than foreach:

No format view Copy to clipboard print code?
Copy Code code 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 certainly slower than while, but in fact, if the array is only read in the loop, then foreach is very fast because the replication mechanism used in PHP is "copy by reference", so it seems that foreach's efficient read operation is not difficult to understand. In addition, since foreach is not suitable for array writes, we can draw the conclusion that in most cases, code similar to a foreach ($arrayas $key=> $value) should be replaced with a while ($key) =each ( $array)).

The speed variance of these techniques may not be obvious in small projects, but in large projects such as frameworks, a request can involve hundreds of thousands of tens of thousands of of array loops at a time, and the difference can be magnified significantly.

for a small example of PHP arrays and loops, including two-dimensional arrays, Yang Hui's triangles, getting parameters, and rectangular diagonal sums, there is a need for friends to suggest a look

Copy Code code as follows:

<?php
1, the use of circular statements, the output of 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 "<br>";
}
echo "<br>";
2, the use of circular control statements, output Yang Hui's 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 digits of each row 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 ' <p> ';
}
}
Yanghuisanjiao (5);
echo "<br>";
3. Use loops and predefined variables to get multiple parameters. The number of parameters is undetermined.
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, use the loop output a two-dimensional array, and the rectangle diagonal elements of the and.
function Getsum ($theCount) {
$b = 0;
Echo ' </p> ';
echo "<table>";
for ($i =1; $i <= $theCount; $i + +) {
echo "<tr>";
for ($j =1; $j <= $theCount; $j + +) {
if ($j = = $i | | $theCount +1-$i = = $j) {
echo "<td style= ' color: #f00 ' > $j </td>";
$b = $b + $j;
if ($j = = $i && $theCount +1-$i = = $j) {
$b = $b + $j;
}
}
else{
echo "<td> $j </td>";
}
}
echo "</tr>";
}
echo "<table>";
echo "The sum of the 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.