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

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

青灯夜游
Release: 2023-03-15 22:38:02
Original
2451 people have browsed it

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

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!

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