2

I need to clean the following PHP array and remove all the empty variables. The array is the made from the string which was copied from a third part software and contains lots of line breaks.

The ways in which I tried and failed are given below.

The array:

Array ( 
[0] => 
[1] => 1.BOB/SMITH RANI MRS
[2] => 2.JON/SMITH MSTR(CHD/01NOV01) 
[3] => 
[4] => 3.BOB/JONES MSTR(CHD/01JUN01)
[5] => 4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2 
[6] => 5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 
[7] => 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 
[8] => 7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2 ) 

function that I tried and failed:

public static function filter_array_empty_value($arr){
    foreach($arr as $k=>$v){
        
        $v = str_replace("\n","",$v);
        $v = str_replace(" ","",$v);
        $v = trim($v);
        
        for($i=1;$i<=50;$i++){
            $v = str_replace("\n","",$v);
            $v = str_replace(" ","",$v);
        }
        
        if($v == " " || $v == NULL || empty($v) || strlen($v)<1){
            unset($arr[$k]);
        }
    }

    return $arr;
}

Note: The array[0] has string length of 4 even after trimming.

Help is appreciated.

Dalmarus
  • 101
  • 6
origin
  • 95
  • 1
  • 1
  • 5

5 Answers5

5

Assuming your array of lines is the result of file() function, just use the flags that own file function supports. Like this:

$filelines = file('filename.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES)

When using the optional flags FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES file function returns all lines except empty lines

felipsmartins
  • 13,269
  • 4
  • 48
  • 56
3
$arr = array ( 
    0 => "",
    1 => "1.BOB/SMITH RANI MRS",
    2 => "2.JON/SMITH MSTR(CHD/01NOV01) ",
    3 => "   ",
    4 => "3.BOB/JONES MSTR(CHD/01JUN01)",
    5 => "4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2\n ",
    6 => "5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 ",
    7 => " 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 ",
    8 => "7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2", 
); 


function filter_array_empty_value($arr){
    foreach($arr as $k=>$v){
        $v = str_replace("\n","",$v);
        $v = str_replace(" ","",$v);
        $v = trim($v);
        $arr[$k] = $v;

        if($v == ""){
            unset($arr[$k]);
        }
    }
    return $arr;
}

$arr2 = filter_array_empty_value($arr);

print_r($arr2);

the output is:

Array
(
    [1] => 1.BOB/SMITHRANIMRS
    [2] => 2.JON/SMITHMSTR(CHD/01NOV01)
    [4] => 3.BOB/JONESMSTR(CHD/01JUN01)
    [5] => 4EK004U26JUL6*LHRDXBHK32040063027JULEEK/GAQNL2
    [6] => 5EK584U27JUL7*DXBDACHK31315195527JULEEK/GAQNL2
    [7] => 6EK583U09SEP2*DACDXBHK31015130509SEPEEK/GAQNL2
    [8] => 7EK005U09SEP2*DXBLHRHK31545201509SEPEEK/GAQNL2
)
Dalmarus
  • 101
  • 6
steven
  • 4,868
  • 2
  • 28
  • 58
0

Nothing simpler then that:

public static function filter_array_empty_value($arr){
    //Output Array
    $output = [];

    //Iterate through each item
    foreach($arr as $item){
        //Make sure, all not printable Chracters are removed (If you want UTF-8 Characters instead of asci use "/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u"
        $item = preg_replace('/[^[:print:]]/', '', $item);
        $item = trim($item);

        //If item is not empty, append it to the Output
        if ($item !== '') {
            $output[] = $item;
        }
    }
    return $output ;
}

As stated in the Comments, we need to delete unprintable Characters:

https://stackoverflow.com/a/8171868/2441442

Community
  • 1
  • 1
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • To be clear, this would remove all lines that have "0" as its value too. Have you tested this? Will it work if the value is " " (two spaces), or even one? – Ascherer May 30 '14 at 16:57
  • This was my primary attempt bro. This did not work as the keys have non printable values. Unfortunately, I dont know how many non-printable characters php support. – origin May 30 '14 at 16:57
0

to remove empty values in array use array_filter

and to recreate sequential indexes use array_values like below

 $arr =Array ( 
"0" => "",
"1" => "1.BOB/SMITH RANI MRS",
"2" => "2.JON/SMITH MSTR(CHD/01NOV01) ",
"3" => "",
"4" => "3.BOB/JONES MSTR(CHD/01JUN01)",
"5" => "4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2 ",
"6" => "5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 ",
"7" => "6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 ",
"8" => "7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2 ");

print_r(array_values(array_filter($arr)));

output

  Array
(
    [0] => 1.BOB/SMITH RANI MRS
    [1] => 2.JON/SMITH MSTR(CHD/01NOV01) 
    [2] => 3.BOB/JONES MSTR(CHD/01JUN01)
    [3] => 4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2 
    [4] => 5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 
    [5] => 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 
    [6] => 7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2 
)
Dalmarus
  • 101
  • 6
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
0

You should probably look at how you are splitting the string to get the array and do it there, but this is pretty simple:

$array = preg_grep('/[^\s]+/', $array);

If there are other invisible characters then add them in the character class [].

If you need to re-index:

$array = array_values(preg_grep('/[^\s]+/', $array));
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87