Home > Backend Development > PHP Problem > How to determine if there are multiple values ​​in an array in php

How to determine if there are multiple values ​​in an array in php

青灯夜游
Release: 2023-03-16 10:38:01
Original
2039 people have browsed it

Judgment method: 1. Use the count() function to obtain the array length, with the syntax "$len=count($arr);"; 2. Use the comparison operator ">" to determine whether the array length is greater than 1. Syntax "$len>1", if the length is greater than 1, there are multiple values ​​in the array, otherwise there is 1 value or no value in the array.

How to determine if there are multiple values ​​in an array in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

php judgment array Is there a method for multiple values

1. Use the count() function to obtain the length of the array

The count() function can count the length of the array The number of all elements

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(1=>"1","a"=>"red","ab"=>"2","b12"=>"green",3=>"3");
var_dump($arr);
$len=count($arr);
echo "数组长度为:".$len;
?>
Copy after login

How to determine if there are multiple values ​​in an array in php

2. Determine whether the array length is greater than 1

The array length is the number of array elements:

  • If the array length is greater than 1, there are multiple values ​​in the array

  • If the array length is less than or equal to 1, there is 1 value in the array Or no value (empty array)

if($len>1){
	echo "数组中有多个值";
}else{
	echo "数组中有1个值或没有值";
}
Copy after login

How to determine if there are multiple values ​​in an array in php

Let’s modify it and delete all the values ​​​​of the $arr array

Copy after login

How to determine if there are multiple values ​​in an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine if there are multiple values ​​in an array 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