php - FTP and pulling item from array want to get rid of an Empty array
one text
Solution:
You can remove those entries with , comparing each name in the array to either
'.'
or '..'
, and only keeping the values that don't match:
$directory = array_filter($directory, function ($name) {
return !in_array($name, array('.', '..'));
});
You could also use to do the comparison, using a regex that will match a string of one or two periods:
$directory = array_filter($directory, function ($name) {
return !preg_match('/^\.\.?$/', $name);
});
Output (for both cases):
Array
(
[2] => .ftpquota
[3] => error_log
[4] => index.php
[5] => index2.php
)
Note if you would prefer a 0-indexed array, just use
$directory = array_values($directory);
after the call to array_filter
.