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 
',

...

4
  • please post your data Commented Mar 25, 2017 at 18:38
  • @LeoTahk Check the solution I provided below Commented Mar 25, 2017 at 19:10
  • yes, the trim worked, thanks for pointing it out. marked as answered. simple array_filter had no effect. Commented Mar 25, 2017 at 19:13
  • why doesn't \x{00a0} get a unicode flag too? Commented Jun 9, 2022 at 0:31

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" 
Sign up to request clarification or add additional context in comments.

Comments

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 ]] )

Comments

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 Comment

I suppose this will not remove values like "\n\n" or "\r\n"
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 )

3 Comments

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 :/
$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 )
the working version for my data is: $array= array_filter($array, function($v){return trim($v);}); $array= array_slice($array, 0 , count($array));

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.