The following small series will bring you an in-depth understanding of the count function of PHP arrays. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.
Count ()
The PHP count () function is used to calculate the number of units in an array or the number of attributes in an object, and returns the number of units in an array or the number of attributes in an object.
Syntax:
Int count (mixed var [, int mode]) If var is a non-array common variable, 1 is returned, and 0 is returned for nonexistent, uninitialized, or empty arrays.
The optional parameter mode is set to COUNT_RECURSIVE (or 1), and count () recursively counts the array, which is particularly useful for calculating all units of a multi-dimensional array, but count () it cannot recognize infinite recursion. The default value of mode is 0.
Example:
<? Phpecho count ($ x); // output: 0 $ a = 2; echo count ($ a); // output: 1 $ arr_age = array (18, 20, 25); echo count ($ arr_age); // output: 3?>
Sizeof () is the alias of this function.
In practice, some cyclic operations are often performed based on the size of the array. we recommend that you write count () in vitro:
<? Php $ arr_age = array (18, 20, 25); $ count = count ($ arr_age); for ($ I = 1; $ I <= $ count; $ I ++) {echo "Nth $ I cycle" ;}?>
In this way, you do not have to execute count () for each loop. of course, this is not necessary.
The above in-depth understanding of the count function of the PHP array is all the content shared by the small editor. I hope to give you a reference, and I hope you can also support the script home.