
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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 Articles
- 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 Array Element and Re-Indexing in PHP
- Removing redundant elements from array altogether - JavaScript
- Removing empty fields from MongoDB
- 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 ?
- Different ways of checking if an array is empty or not in PHP
- Find minimum possible size of array with given rules for removing elements in C++
- Removing least number of elements to convert array into increasing sequence using JavaScript
- Removing item from array in MongoDB?

Advertisements