Use PHP for loop to run array result

 If you have read the article on PHP foreach , you may feel that using PHP for loop to run arrays seems a bit of a fart off your pants, but sometimes those who are used to using for loop will still use it. It is practical for loop. It's not impossible to run, as long as you add a PHP count to count the number of elements in the array, you can also run the desired result.


Use PHP for loop to run array example
<?php
$Arr=array('a','b','c');
$i=count($Arr);
for($j=0 ; $j<$i ; $j++){
 echo '第 '.$j.' 圈 : '.$Arr[$j].'<br>';
}
?>
In the



example of presenting the results , we first write a simple $Arr array, then use count to calculate the number of array elements and store it as a variable $i, so that it can be put into the for loop to run, and in the for loop we write another The variable $j starts from 0 and increases by one for every lap. As long as $j <$i, the for loop will continue to run, and the array elements of $Arr can be output smoothly!

This way of writing requires one more action of count. If you want to simplify the way of writing, you can use PHP foreach to directly output the array, which is simple and saves the resource consumption of count calculation. It is worth spending some time to study, program or data volume The larger the size, the more need to focus on efficiency.

Post a Comment

0 Comments