Remove Empty Array Elements

Remove empty array elements

As you're dealing with an array of strings, you can simply use array_filter(), which conveniently handles all this for you:

print_r(array_filter($linksArray));

Keep in mind that if no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed. So if you need to preserve elements that are i.e. exact string '0', you will need a custom callback:

// PHP 7.4 and later
print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return !is_null($value) && $value !== ''; }));

// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));

Note: If you need to reindex the array after removing the empty elements, use: $linksArray = array_values(array_filter($linksArray));

Remove empty elements from an array in Javascript

EDIT: This question was answered almost nine years ago when there were not many useful built-in methods in the Array.prototype.

Now, certainly, I would recommend you to use the filter method.

Take in mind that this method will return you a new array with the elements that pass the criteria of the callback function you provide to it.

For example, if you want to remove null or undefined values:

var array = [0, 1, null, 2, "", 3, undefined, 3,,,,,, 4,, 4,, 5,, 6,,,,];
var filtered = array.filter(function (el) { return el != null;});
console.log(filtered);

How to remove empty array values () from an array?

You could use the filter like:

arr = arr.filter(item => item);

Example:

let arr = ['One', 'Two', '', 'Four', '', ''];
arr = arr.filter(item => item);
console.log(arr);

// Result
// ['One', 'Two', 'Four']

Because an empty string evaluates to boolean false.
It works with all falsy values like 0, false, null, undefined, '', etc.

DEMO

If you want to keep some values like number 0 (zero) you could use item !== undefined. This filters only undefined values. Keep in mind to trim your string or check with regex to ensure empty strings without whitespaces.

php remove empty values in array

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

Remove empty values from PHP array but keep 0

Assumption: I think you want to remove NULL as well as empty-strings/values '' from your array. (What i understand from your desired output)

You have to use array_filter() with strlen()

array_filter($array,'strlen');

Output:-

https://eval.in/926585

https://eval.in/926595

https://eval.in/926602

Refrence:-

PHP: array_filter - Manual

PHP: strlen - Manual

How do I remove blank elements from an array?

There are many ways to do this, one is reject

noEmptyCities = cities.reject { |c| c.empty? }

You can also use reject!, which will modify cities in place. It will either return cities as its return value if it rejected something, or nil if no rejections are made. That can be a gotcha if you're not careful (thanks to ninja08 for pointing this out in the comments).



Related Topics



Leave a reply



Submit