in PHP by (13.3k points)
0 like 0 dislike
10 views

How I can to delete empty elements of array in PHP?


php delete empty array elements
php remove empty array elements

1 Answer

0 like 0 dislike
by (13.3k 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);

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.