Home  >  Article  >  Backend Development  >  How to check if there is a qualified value in an array in php

How to check if there is a qualified value in an array in php

青灯夜游
青灯夜游Original
2022-09-30 17:12:243038browse

Detection steps: 1. Customize a callback function to set the conditions for processing array elements, the syntax is "function f($num){return(specified condition);}"; 2. Call using array_filter() Callback function to filter the array and obtain elements that meet the conditions, the syntax is "array_filter($arr,"f")"; 3. Use count() to filter to obtain the length of the array, and determine whether the length of the array is 0. If it is 0, the array There are no elements that meet the conditions in , and vice versa.

How to check if there is a qualified value in an array in php

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

In php, you can use array_filter() and The count() function is used to detect whether there are qualified values ​​in the array

Detection steps:

Step 1: Customize one The callback function is used to set the conditions for processing array elements and return elements that meet the conditions

function f($num){
	 return(指定条件);
}

Step 2: Use the array_filter() function to call the callback function to filter the array and obtain the elements that meet the conditions

  • array_filter() filters the array and returns the elements of the symbolic condition

array_filter($arr,"f");

will return a filtered array containing all elements that meet the condition

Step 3: Use the count() function to obtain the length of the filter array and determine whether the array length is 0

count(原数组)===0
  • If it is 0, then There is no element in the array that matches the addition

  • If it is not 0, there is an element in the array that matches the addition

Complete example code : Detect whether there is a value in the array that meets the conditions (greater than 0)

0); //条件是 大于0的元素
}
$arr=array(2,-1,0,-8,-10,-5,9);
var_dump($arr);

echo "过滤后的数组:";
$res=array_filter($arr,"f");
var_dump($res);

if(count($arr)===0){
	echo "数组中没有符合条件的元素";
}else{
	echo "数组中有符合条件的元素";
}
?>

How to check if there is a qualified value in an array in php

Description:

array_filter() function

array_filter() function uses the callback function to filter the elements in the array and returns a filtered array.

This function passes each key value in the input array to the callback function. If the callback function returns true, the current key value in the input array is returned to the result array. Array key names remain unchanged.

array array_filter ( $array , 回调函数 )

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to check if there is a qualified value in an array 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