如何用 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 )
广告