in PHP by (15.1k points)
0 like 0 dislike

Some elements in my array are empty. I need remove these empty array elements. How I can to delete empty elements of array in PHP PL?

Suitable search queries for this question
  • php delete empty array elements
  • php remove empty array elements
  • php purify array from empty elements

1 Answer

0 like 0 dislike
by (15.1k points)

You can use array_filter to remove empty elements:

$emptyRemoved = array_filter($linksArray);

If you have (int) 0 in your array, you may use the following:

$emptyRemoved = remove_empty($linksArray);

function remove_empty($array) {
  return array_filter($array, '_remove_empty_internal');

function _remove_empty_internal($value) {
  return !empty($value) || $value === 0;

If your elements are not empty, e.g., contain one or more spaces, then you can use the following before using array_filter:

$trimmedArray = array_map('trim', $linksArray);

Please log in or register to answer this question.