Home > Backend Development > PHP Problem > How to determine whether array elements are empty in php

How to determine whether array elements are empty in php

青灯夜游
Release: 2023-03-16 13:26:02
Original
1968 people have browsed it

Implementation steps: 1. Use the array_filter() function to filter the array and delete the empty elements in the array. The syntax "array_filter (original array)" will return a filtered array; 2. Use count() The function obtains the length of the original array and the length of the filtered array; 3. Check whether the lengths of the two obtained arrays are equal. The syntax is "original array length === filtered array length". If they are equal, the array elements are not empty, and vice versa. Some array elements are empty.

How to determine whether array elements are empty in php

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

In php, you can use the array_filter() function and count() determine that none of the array elements are empty.

Implementation idea:

  • Use the array_filter() function to delete empty elements in the array

  • Use the count() function to obtain the original array length and the processed array length, and determine whether the two lengths are equal

Implementation steps:

1. Use the array_filter() function to filter the array and delete empty elements in the array.

The array_filter() function, also known as the callback function, is used to filter the array using user-defined functions. Elements. It iterates over each value in the array, passing them to a user-defined function or callback function.

When the array_filter() function is used to declare a callback function, it will delete false values ​​(null values), but if the callback function is not specified, all values ​​equal to FALSE in the array will be deleted, such as the null character String or NULL value.

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(11,&#39;&#39;,null,12,false,0); 
var_dump($arr);
$newarr = array_filter($arr); 
echo "过滤后的数组:";
var_dump($newarr);
?>
Copy after login

How to determine whether array elements are empty in php

2. Use the count() function to obtain the length of the original array and the length of the filtered array

$len1=count($arr);
$len2=count($newarr);
Copy after login

3. Determine whether the two lengths are equal

  • If the two lengths are equal, the empty elements are not deleted, that is, none of the array elements are empty

  • If the two lengths are not equal, the empty elements are deleted, that is, some array elements are empty

$len1=count($arr);
$len2=count($newarr);
if($len1===$len2){
	echo "数组元素均不为空值";
}
else{
	echo "有数组元素为空值";
}
Copy after login

Implementation example code:

<?php
header("Content-type:text/html;charset=utf-8");
function f($arr){
	$len1=count($arr);
	$newarr = array_filter($arr); 
	$len2=count($newarr);
	if($len1===$len2){
		echo "数组元素均不为空值<br>";
	}else{
		echo "有数组元素为空值<br>";
	}
}


$arr1 = array(11,&#39;&#39;,null,12,false,0); 
var_dump($arr1);
f($arr1);

$arr2 = array(1,2,3,4,5,6); 
var_dump($arr2);
f($arr2);
?>
Copy after login

How to determine whether array elements are empty in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine whether array elements are empty 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