The PHP array loop operation details the attached instance code

  • 2020-05-27 04:35:42
  • OfStack

The PHP array is still quite common, so I studied the PHP array loop operation of 1, and took it out here to share with you 1, I hope it is useful for you. PHP is basically an array language. PHP array of every so often a sample is a large number of loop operation, there are two main ways, 1 kind is foreach, another one is while, what kind of what kind of bad 1 straight, the argument although I have long been aware of this problem, but there is no more, 1 straight feel ignorant 1 straight until now, in order to later can save CPU time, the following summary 1:

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

 
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]; 
} 

If the loop is "write" to the array, while is faster than foreach:

Unformatted view copy to clipboard to print code?
 
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].'...'; 
} 

Conclusion: it is generally believed that foreach involves value copying, and 1 must be slower than while, but in fact, if you are only reading an array in a loop, then foreach is very fast. This is because PHP USES a copy mechanism of "copy by reference, copy by write", which makes it easy to understand the efficient read operation of foreach. In addition, since foreach is not suitable for array write operations, we can conclude that in most cases, it is similar to foreach($arrayas$key=) > All code in the form of $value should be replaced with while(list($key)=each($array)).

The difference in the speed of these techniques may not be obvious in a small project, but in a large project such as a framework, where a single request often involves hundreds of thousands of group cycles, the difference is magnified.

Small example of php array and loop, including 2 - dimensional array, Yang hui 3 Angle, get parameters, rectangle diagonal sum, need to have a friend's suggestion to see


<?php
//1 , the use of a loop statement, arbitrary output 1 a 2 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 $val1){
echo "$val1&nbsp;";
}
echo "<br>";
}
echo "<br>";
//2 , use the loop control statement, output Yang hui 3 Angle. 
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; // Put the first of each line 1 And finally 1 Let's set the number as 1
}else{
$sc[$i][$j]=$sc[$i-1][$j-1]+$sc[$i-1][$j];
}
}
}
foreach ($sc as $value){
foreach($value as $v1){
echo $v1.' ';
}
echo '<p>';
}
}
yanghuisanjiao(5);
echo "<br>";
//3 Get multiple parameters using loops and predefined variables. The number of parameters is undetermined. 
function avg(){
$ags=func_get_args();
$sum=0;
foreach ($ags as $v){
$sum+=$v;
}
return ' The average is: '.$sum/func_num_args();
}
echo avg(1,2,3,4,5,6,7);
//4 , using cyclic output 1 a 2 Dimension array and find the sum of the diagonal elements of the rectangle. 
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 zero :".$b;
}
getSum(6);
?>


Related articles: