- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 −
<?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>"); ?>
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.
- Removing duplicate elements from an array in PHP
- Adding and Removing Elements in Perl Array
- Removing empty fields from MongoDB
- Removing Array Element and Re-Indexing in PHP
- 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?
- Find minimum possible size of array with given rules for removing elements in C++