Thursday, November 27, 2014

Check How to Remove Empty Array Elements PHP

There are mainly two types of the way to delete empty values from an array. First way is logical but little slow. However, Second example is the fastest way to do this. So, I recommend using the second way.

Remove Empty Array Elements PHP


1. Slow and Simple Way :-

The most straight forward way of doing this.

<?php

// our massive array with set values and empty values
$array = array(...);

foreach( $array as $key => $value )
        if( $value == "" ) unset($array[$key]);

?>

2. Fastest Way to Delete Empty Array Elements PHP :-

Below we fetch a list of keys from the array that is empty (using the php function array_keys) and then unset each of those values.

<?php

// our massive array with set values and empty values
$array = array(...);

// get the empty array keys using array_keys
$keys = array_keys($array,"");

// foreach empty key, we unset that entry
foreach ($keys as $k)
        unset($array[$k]);

?>

0 comments:

Post a Comment