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