How to Remove Empty Array Elements and reindex in PHP Example?

How to Remove Empty Array Elements and reindex in PHP Example?

In this post, I will tell you how to remove empty values from array in PHP by using array_filter function.

array_filter function remove all elements from given array that are equal to boolean false and emtpy string always return boolean false.

So question is it that 0 is also return boolean false then array_filter will remove 0 too then how will you keep it in array ?

Example 1: array_filter() function
  1. $my_array = array(0, "ExpertPHP", "","Demo");
  2. $resultDirect = array_filter($my_array);
  3. print_r($resultDirect);

Output :

  1. Array
  2. (
  3. [1] => ExpertPHP
  4. [3] => Demo
  5. )
Example 2: array_diff() function
  1. $my_array = array(0, "ExpertPHP", "","Demo");
  2. echo "
    ";
  3. print_r( array_diff( $my_array, array( '' ) ) );

Output:

  1. Array
  2. (
  3. [0] => 0
  4. [1] => ExpertPHP
  5. [3] => Demo
  6. )

array_diff() function return all the element of first array except element that present in second array.

Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort sakarya escort sakarya escort serdivan escort izmir escort eporner
akyazı escort sakarya escort ferizli escort sakarya escort geyve escort sakarya escort hendek escort sakarya escort karasu escort sakarya escort sapanca escort serdivan escort webmaster forum
pendik escort atasehir escort