6

I'm trying to clean an array from empty values by using the following function:

function remove_empty_cells($data)
{
    $findthis = array("/\r\n/u", "/\n/u", "/\r/u", "/\s+/u", "~\x{00a0}~");
    for($i=0; $i<count($data); $i++)
    {
        $data[$i] = preg_replace($findthis, " ", $data[$i]);
        $data[$i] = htmlspecialchars_decode(htmlspecialchars($data[$i], ENT_SUBSTITUTE, 'UTF-8'));
        if (empty($data[$i]) ||
            mb_strlen($data[$i]) < strlen($data[$i]) ||
            is_null($data[$i]) ||
            $data[$i] = " " ||
            $data[$i] = "" ||
            $data[$i] == "0")
        {
             array_splice($data, $i, 1);
        };//end if
    };//end for
    return $data;
};//end func

The empty values don't go away, I fail to identify them..

the data:

array (
  0 => '
',
  1 => 'BEGIN:VCARD 
',
  2 => '
',
  3 => '
',
  4 => '
',
  5 => '
',
  6 => '
',
  7 => 'VERSION:2.1 
',

... might be some encoding problem or something.. it doesnt look like multibyte characters.. the result i get is:

array (
  0 => 'BEGIN:VCARD 
',
  1 => '
',
  2 => '
',
  3 => 'VERSION:2.1 
',

...

5

4 Answers 4

12

You can use the PHP function array_filter() to remove the empty values

<?php
$array = array("sampe1", "", "sample2", null, "sample4", "rajesh", "suresh", "test", "");
var_dump($array);
echo "<hr/>";
$result = array_filter($array);                 
var_dump($result);
?>

This will print out:

array(9) { [0]=> string(6) "sampe1" [1]=> string(0) "" [2]=> string(7) "sample2" [3]=> NULL [4]=> string(7) "sample4" [5]=> string(6) "rajesh" [6]=> string(6) "suresh" [7]=> string(4) "test" [8]=> string(0) "" }

array(6) { [0]=> string(6) "sampe1" [2]=> string(7) "sample2" [4]=> string(7) "sample4" [5]=> string(6) "rajesh" [6]=> string(6) "suresh" [7]=> string(4) "test" 
0
10

You can use the PHP function array_filter() to remove the empty values with callback function as below

<?php
    $array = array("sampe1", "\n  ", "   ", "sample2", null, "sample4", "rajesh", "suresh", "test", "");

    $result = array_filter($array, function($v){
       return trim($v);
    });

    var_dump($result);

    $res = array_slice($result);

    var_dump($res);
?>

This will print out:

array(6) { [0]=> string(6) "sampe1" [3]=> string(7) "sample2" [5]=> string(7) "sample4" [6]=> string(6) "rajesh" [7]=> string(6) "suresh" [8]=> string(4) "test" }

array(6) { [0]=> string(6) "sampe1" [1]=> string(7) "sample2" [2]=> string(7) "sample4" [3]=> string(6) "rajesh" [4]=> string(6) "suresh" [5]=> string(4) "test" }

SYNTAX - array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )

2

Not very sure what you did in that code of yours but here is something that can remove empty elements from a php array

Code Snippet

<?php
   $array = array("apple", "", 2, null, -5, "orange", 10, false, "");
   var_dump($array);
   echo "<br>";

   // Filtering the array
   $result = array_filter($array);                 
   var_dump($result);
?>
1
  • 2
    I suppose this will not remove values like "\n\n" or "\r\n"
    – u_mulder
    Mar 25, 2017 at 18:45
2

Removing empty array slots in PHP, leaving holes in the array.

A quick way to remove empty elements from an array is using array_filter without a callback function. This will also remove 0s (zeroes) though.

$myArray = array_filter( $myArray );

Alternatively, array_diff allows you to decide which elements to keep. The following example will only remove empty strings, but keep 0

$myArray = array_diff( $myArray, array( '' ) );

Removing empty array slots in PHP, and compacting the array.

Both functions leave ‘gaps’ where the empty entries used to be. You can see it below, where the indices are [1] and [3] and not [0] and [1].

$myArray = array( 0, 'red', '', 'blue' ); print_r( array_filter( $myArray ) ); Array ( [1] => 'red' [3] => 'blue' )

print_r( array_diff( $myArray, array( '' ) ) ); Array ( [0] => 0 [1] => 'red' [3] => 'blue' )

array_slice can remove those gaps:

$myArray = array( 0, 'red', '', 'blue' ); $myArray = array_filter( $myArray ); print_r( array_slice( $myArray, 0 ) ); Array ( [0] => 'red' [1] => 'blue' )

so now for your cas, Example

$arr = ['','BEGIN:VCARD','','','','','','VERSION:2.1']; //initial array print_r($arr); $myArray = array_filter( $arr ); print_r( array_slice( $myArray, 0 ) );

inital output:

Array ( [0] => [1] => BEGIN:VCARD [2] => [3] => [4] => [5] => [6] => [7] => VERSION:2.1 )

final ouput :

Array ( [0] => BEGIN:VCARD [1] => VERSION:2.1 )

4
  • i just used $fileContents = array_filter( $fileContents ); print_r( array_slice( $fileContents, 0 ) ); the result is: Array ( [0] => [1] => BEGIN:VCARD [2] => [3] => [4] => [5] => [6] => [7] => VERSION:2.1 .. not removed :/
    – Leo Tahk
    Mar 25, 2017 at 19:06
  • $arr = ['','BEGIN:VCARD','','','','','','VERSION:2.1']; NEXT: print_r($arr); $myArray = array_filter( $arr ); print_r( array_slice( $myArray, 0 ) ); inital output: Array ( [0] => [1] => BEGIN:VCARD [2] => [3] => [4] => [5] => [6] => [7] => VERSION:2.1 ) final ouput : Array ( [0] => BEGIN:VCARD [1] => VERSION:2.1 )
    – Sylvester
    Mar 25, 2017 at 19:23
  • the working version for my data is: $array= array_filter($array, function($v){return trim($v);}); $array= array_slice($array, 0 , count($array));
    – Leo Tahk
    Mar 25, 2017 at 19:43
  • simple solution: stackoverflow.com/a/66004034/7186739 Oct 10, 2021 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.