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