Home  >  Article  >  Backend Development  >  How to check how many values ​​there are in a php array

How to check how many values ​​there are in a php array

DDD
DDDOriginal
2023-06-02 09:38:36692browse

There are 5 values ​​in the php array. Query method: 1. Create a PHP sample file; 2. Create a counter variable and set the value to 0; 3. Use the syntax "foreach($arr as $v)" to loop Traverse the array; 4. Use the syntax "if(is_numeric($v)){$n;}" to determine whether the array element is a numeric value. If so, the counter will increase by 1; 4. The loop ends and the counter is output.

How to check how many values ​​there are in a php array

The operating environment of this tutorial: Windows 10 system, php8.1.3 version, dell g3 computer

How many values ​​does the php array have? Check

php to check how many values ​​an array contains and calculate the number of numerical elements in the statistical array.

Implementation method:

Create and initialize a counter variable with the value 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 a numeric value; if it is a numeric value, the counter will increase by 1.

The loop ends and the counter is output.

Implementation code:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$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." 个";
?>

How to check how many values ​​there are in a php array

It can be seen that there are 5 numerical elements in the array.

The above is the detailed content of How to check how many values ​​there are in a php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn