Method: 1. Create a counter variable and set the value to 0; 2. Loop through the array with the syntax "foreach($arr as $v)"; 3. In the loop body, determine whether the element is a numeric value. If If so, the counter will be incremented by 1, syntax if(is_numeric($v)){$n;}"; 4. The loop ends and the counter will be output.
This tutorial Operating environment: Windows 7 system, PHP version 7.1, DELL G3 computer
How does php check how many values an array has?
php checks one The array has several values. Calculate the number of numerical elements in the statistical array.
Implementation method:
Create and initialize a counter variable, The value is set to 0 for counting.
Use foreach loop to traverse the array
In the loop body, use the is_numeric function to determine whether the array element is is a numerical value; if it is a numerical value, the counter will increment by 1.
The loop ends and the counter is output.
Implementation code :
<?php header('content-type:text/html;charset=utf-8'); $arr= array(1,"a",3,"b",5,"cc",7,"d",null,10); var_dump($arr); $n=0; foreach($arr as $v){ if(is_numeric($v)){ $n++; } } echo "数值元素有: ".$n." 个"; ?>
It can be seen that there are 5 numerical elements in the array.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to check how many values an array has in php. For more information, please follow other related articles on the PHP Chinese website!