
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Null Values with PHP
To remove null value in PHP, use array_filter(). It filters the array values. Let’s say the following is our array −
$studentDetails = array("firstName" => "John", "lastName"=> null); echo "The original value is=";print_r($studentDetails);
Let’s filter with array_filter() −
$result = array_filter($studentDetails);
Example
<!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>
Output
The original value is=Array ( [firstName] => John [lastName] => ) After removing null part,the result is=Array ( [firstName] => John )
Advertisements