php for loop traverses index array
The word "traverse" literally means to read and access them all one after another and display them.
Because the for loop is a simple counting loop, and the subscript of the index array is an integer value. Therefore, we can iterate through the index array through a for loop.
We know that the index array subscript is an integer. We define the following array:
<?php //聲明一個數(shù)組,值為1到10 $num = array(1,2,3,4,5,6,7,8,9,10); //按照索引數(shù)組的特點,下標從0開始。所以1的下標為0,10的下標為9 echo $num[0].'<br />'; echo $num[9].'<br />'; //我們可以得到數(shù)組中元素的總個數(shù),為10 echo count($num); //遍歷這個索引數(shù)組的話,我們就可以定義一個變量為$i //$i 的值為0,從0開始 //可以設定一個循環(huán)條件為:$i 在下標的(9)最大值之內循環(huán) for($i = 0 ; $i < count($num) ; $i++){ echo $num[$i].'<br />'; } ?>
Through the above example, we loop the array.
Because the subscript starts from 0, define $i=0. Let $i increase by 1 each time it loops, but it must be less than 10, because the maximum value of the array subscript is 9.
In this way, we have learned to traverse the indexed consecutive subscript array.
Then the question is:
What to do with associative arrays? What if the subscripts of the index array are not consecutive?
Answer: We will talk about it in the next chapter, young man, don’t worry.