如何用 PHP 删除空值?
在 PHP 中删除空值,请使用 array_filter()。它会过滤数组值。假设我们的数组如下所示 −
$studentDetails = array("firstName" => "John", "lastName"=> null);
echo "The original value is=";print_r($studentDetails);让我们使用 array_filter() 过滤 −
$result = array_filter($studentDetails);
示例
<!DOCTYPE html>
<html>
<body>
<?php
$studentDetails = array("firstName" => "John", "lastName"=> null);
echo "The original value is=";
print_r($studentDetails);
$result = array_filter($studentDetails);
echo "</br>";
echo "After removing null part,the result is=";
print_r($result);
?>
</body>
</html>输出
The original value is=Array ( [firstName] => John [lastName] => ) After removing null part,the result is=Array ( [firstName] => John )
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP