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

$linksArray = 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));
Answer from BoltClock on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.array-filter.php
PHP: array_filter - Manual
The result array can be reindexed using the array_values() function. ... If no callback is supplied, all empty entries of array will be removed. See empty() for how PHP defines empty in this case. ... ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback instead of the value ...
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-remove-empty-values-from-an-array-in-php.php
How to Remove Empty Values from an Array in PHP
However, if no callback function is specified, all empty entries of array will be removed, such as "" (an empty string), 0 (0 as an integer), 0.0 (0 as a float), "0" (0 as a string), NULL, FALSE and array() (an empty array).
๐ŸŒ
TecAdmin
tecadmin.net โ€บ removing-empty-values-from-array-in-php
Removing Empty Values from an Array in PHP โ€“ TecAdmin
April 26, 2025 - One of the most common ways to remove empty values from an array in PHP is to use the `array_filter()` function. It filters the elements of an array using a callback function.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ program-to-remove-empty-array-elements-in-php
Program to remove empty array elements in PHP - GeeksforGeeks
July 11, 2025 - It is achieved by using array_filter() function. It also removes false values when declared using a callback function, however, if no callback function is specified, all the values of the array which are equal to FALSE will be removed, such ...
๐ŸŒ
W3Docs
w3docs.com โ€บ php
How to Remove Empty Array Elements in PHP
<?php $array = [0, 'amir', 'test', 490, null, '', 'Hello world']; $array = array_values(array_filter($array)); var_dump($array); ?>
๐ŸŒ
Uptimia
uptimia.com โ€บ home โ€บ questions โ€บ how to remove empty elements from an array in php?
How To Remove Empty Elements From An Array In PHP?
October 10, 2024 - This line removes elements PHP considers empty, including empty strings, null values, and false. ... Simple: It needs little code, making scripts easier to read. Fast: It works quicker than manual loops, especially for big arrays. Built-in: It's part of PHP, so you don't need extra libraries. If you want to keep the original array keys after filtering, you can pass the ARRAY_FILTER_USE_BOTH flag as the third argument to array_filter():
๐ŸŒ
The Code Developer
thecodedeveloper.com โ€บ home โ€บ php โ€บ how to remove empty array elements in php
Remove empty array elements from an array in PHP - PHP Tutorial
December 4, 2018 - Use the PHP array_filter() function remove empty array elements from an array in PHP. This will also remove blank, null, false, 0 (zero) values.
Find elsewhere
๐ŸŒ
CodexWorld
codexworld.com โ€บ home โ€บ how to guides โ€บ how to remove empty elements from multidimensional array in php
How to Remove Empty Elements from Multidimensional Array in PHP - CodexWorld
October 2, 2023 - The array_map() and array_filter() function can helps to filter empty elements recursively from multidimensional using PHP. Use the array_map() function with array_filter callback to filter each array in multidimensional array.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ php โ€บ how to remove empty array elements in php
How to Remove Empty Array Elements in PHP | Delft Stack
February 2, 2024 - ... The built-in function array_filter() removes all the empty elements, zeros, false and null values from an array. This function uses a callback function to filter the array values.
๐ŸŒ
Webstuffsolution
webstuffsolution.com โ€บ how-to-remove-empty-values-from-array-in-php
How to remove empty values from array in PHP?
September 19, 2024 - The array_filter() function filters the elements of an array using callback function. This function removes all empty values from the array. You can easily use the PHP array_filter() function to remove or filter empty values from an array.
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Filtering Empty Values in Array - PHP - SitePoint Forums | Web Development & Design Community
January 14, 2011 - Hi guys, Iโ€™m trying to filter an array which contains all of my POST values from a form that I have put into a session. Trying to filter out empty values but just returning the same values which are empty. $details = array_filter($_SESSION['DETAILS']); print_r($details); See below of my output: Array ( [ctitle] => Mrs [cfirstname] => Joe [clastname] => Bloggs [caddress] => 23 Joe Blogg Street, Devon [ccontactnumber] => 01234 039213 [cemail] => coxdabd@gmail.com [receiptno] => KK3 [delmethod...
๐ŸŒ
Saywebsolutions
saywebsolutions.com โ€บ blog โ€บ remove-empty-array-elements-in-php
Remove Empty Array Elements in PHP - Say Web Solutions
// removes null, false, and empty strings, but leaves 0 (zero) values $result = array_filter( $array, 'strlen' ); โ‹ PHP โ‹ PHP snippets โ‹ PHP one-liners โ‹ PHP arrays
๐ŸŒ
GitHub
github.com โ€บ particle-php โ€บ Filter โ€บ issues โ€บ 57
How to keep empty values in filtered array? ยท Issue #57 ยท particle-php/Filter
November 2, 2017 - What I've found that Filter removes an empty value from the input array after you apply any filter on it: $filter = new Filter(); $filter->all()->trim(); $result = $filter->filter([ 'one' => ' 1 ', 'zero' => ' 0 ', 'space' => ' ', 'null'...
Published ย  Dec 11, 2017
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-check-whether-an-array-is-empty-using-php
How to Check Whether An Array is Empty Using PHP? - GeeksforGeeks
June 25, 2025 - Use array_filter() when dealing with arrays containing empty, null or false values to filter out unwanted element ยท There are multiple ways to check if an array is empty or not in PHP, each serving a unique purpose depending on the context of your project. The === [] syntax provides a strict check for an empty array, while the empty() function is useful for detecting empty values and other falsy data types...
๐ŸŒ
PHP 101
php101.net โ€บ how-to โ€บ remove-empty-array-elements
How to remove empty array elements using PHP | PHP 101
November 7, 2025 - Quick solution The quickest way to remove empty array elements in a PHP array is using the array_filter() function: array_filter(
๐ŸŒ
bishrulhaq
bishrulhaq.com โ€บ posts โ€บ remove-array-elements-in-php
PHP Program to remove empty or specific array elements | BH
Syntax : array array_filter($array, $callback_function, $flag) Below is a program showing how to return filtered array elements using array_filter() function. <?php // Declaring the array $array = array("bishrul", 27, '', null, "developer", ...