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

DDD
Release: 2023-06-02 10:29:39
Original
699 people have browsed it

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." 个";
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template