如何使用 PHP 移除空值?
phpserver side programmingprogramming更新于 2025/6/25 19:07:17
要在 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 )

