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

更新日期:2020 年 10 月 12 日

604 人瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.