How php iterates over the number of elements in an array
This article mainly explains "how to iterate the number of array elements in php". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "php how to iterate the number of array elements".
Implementation: 1, define a counter variable, syntax "$len=0;"; 2, loop through the array, syntax "foreach ($arr as $v) {}"; 3, in the body of the loop, count the number with the "$len++;" statement, each cycle, the counter value will be added 1 × 4, the end of the loop, the output counter value can be.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Php loop to calculate the number of array elements
In php, when you mention circular arrays, you think of for and foreach statements.
However, when the for statement loops through the array, you need to know the length of the array, so this article does not apply.
So we need to use the foreach statement to loop through the array.
Realize the idea:
Define a counter variable $len with a value initialized to 0, which is used to count the number of array elements.
Use the foreach statement to loop through the array
In the body of the loop, use the "+" operator to calculate the number, and add 1 to the counter value for each loop.
At the end of the loop, output the value of the counter.
Implementation code:
Is it correct? Let's use the built-in function to verify:
The number of elements in the echo "array is:" .count ($arr)
At this point, I believe you have a deeper understanding of "php how to iterate the number of array elements". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!