Home  >  Article  >  Backend Development  >  How to check how many values ​​an array has in php

How to check how many values ​​an array has in php

青灯夜游
青灯夜游Original
2022-05-07 14:54:352422browse

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.

How to check how many values ​​an array has in php

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(&#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 ​​an array has in php

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!

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