

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Removing empty array elements in PHP
To remove empty array elements in PHP, the code is as follows −
Example
<?php $my_array = array("This", 91, '', null, 102, "is", false, "a", "sample", null); foreach($my_array as $key => $val) if(empty($val)) unset($my_array[$key]); echo "After removing null values from the array, the array has the below elements -"; foreach($my_array as $key => $val) echo ($my_array[$key] ."<br>"); ?>
Output
After removing null values from the array, the array has the below elements -This 91 102 is a sample
An array is defined, that contains strings, numbers and ‘null’ values. The ‘foreach’ loop is used to iterate over the elements and if a value is empty, i.e. it contains null, then it is deleted from the array. The relevant array is displayed again that wouldn’t contain null values.
- Related Questions & Answers
- Removing duplicate elements from an array in PHP
- Removing all the empty indices from array in JavaScript
- Adding and Removing Elements in Perl Array
- Removing empty fields from MongoDB
- Removing Array Element and Re-Indexing in PHP
- Removing redundant elements from array altogether - JavaScript
- Removing duplicates and inserting empty strings in JavaScript
- Program to find mean of array after removing some elements in Python
- How to check whether an array is empty using PHP?
- Remove duplicated elements of associative array in PHP
- Reset keys of array elements using PHP ?
- Removing Single Elements in a Vector in Java
- Removing item from array in MongoDB?
- Removing Negatives from Array in JavaScript
- Find minimum possible size of array with given rules for removing elements in C++
Advertisements