Skip to main content
infervour.com

Back to all posts

How to Delete an Empty Array In PHP?

Published on
4 min read
How to Delete an Empty Array In PHP? image

Best PHP Tools to Buy in October 2025

1 PHP & MySQL: Server-side Web Development

PHP & MySQL: Server-side Web Development

BUY & SAVE
$29.21 $45.00
Save 35%
PHP & MySQL: Server-side Web Development
2 PHP and MySQL: The Comprehensive Guide to Server-Side Web Development with PHP 8 – Build Dynamic Websites with Database Integration, Security, and More (Rheinwerk Computing)

PHP and MySQL: The Comprehensive Guide to Server-Side Web Development with PHP 8 – Build Dynamic Websites with Database Integration, Security, and More (Rheinwerk Computing)

BUY & SAVE
$38.82 $59.95
Save 35%
PHP and MySQL: The Comprehensive Guide to Server-Side Web Development with PHP 8 – Build Dynamic Websites with Database Integration, Security, and More (Rheinwerk Computing)
3 Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

BUY & SAVE
$39.03 $65.99
Save 41%
Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)
4 PHP in easy steps: Updated for PHP 8

PHP in easy steps: Updated for PHP 8

BUY & SAVE
$13.86 $16.99
Save 18%
PHP in easy steps: Updated for PHP 8
5 Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites

Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites

BUY & SAVE
$43.61 $59.99
Save 27%
Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites
6 Programming PHP: Creating Dynamic Web Pages

Programming PHP: Creating Dynamic Web Pages

BUY & SAVE
$36.49 $65.99
Save 45%
Programming PHP: Creating Dynamic Web Pages
7 PHP: Learn PHP in One Day and Learn It Well. PHP for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 6)

PHP: Learn PHP in One Day and Learn It Well. PHP for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 6)

BUY & SAVE
$3.99
PHP: Learn PHP in One Day and Learn It Well. PHP for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 6)
8 PHP and MySQL Web Development (Developer's Library)

PHP and MySQL Web Development (Developer's Library)

BUY & SAVE
$41.99 $59.99
Save 30%
PHP and MySQL Web Development (Developer's Library)
9 PHP Crash Course: The Complete, Modern, Hands-On Guide

PHP Crash Course: The Complete, Modern, Hands-On Guide

BUY & SAVE
$53.53 $69.99
Save 24%
PHP Crash Course: The Complete, Modern, Hands-On Guide
+
ONE MORE?

In PHP, you can use the unset() function to delete an empty array. The unset() function is used to unset a given variable or element in an array.

To delete an empty array, simply call the unset() function and pass the array variable as the argument. This will remove the array from memory, freeing up the allocated space.

Here's an example:

$emptyArray = array(); unset($emptyArray);

In the above code, we declare an empty array called $emptyArray. Then, we use the unset() function to remove this array from memory.

It is important to note that the unset() function will completely remove the array and you won't be able to access it anymore. Therefore, it's crucial to make sure that you no longer need the array before using unset().

Additionally, if the array is not empty and you want to delete all its elements, you can do so by using the array splice() or array_slice() functions.

For example, using array_splice():

$nonEmptyArray = array(1, 2, 3, 4, 5); array_splice($nonEmptyArray, 0);

In the above code, we have a non-empty array called $nonEmptyArray. By calling array_splice() with a start index of 0, we remove all the elements from the array, effectively making it empty.

Remember to use these array deletion methods with caution to ensure you don't unintentionally remove important data.

How to delete all occurrences of an element from a multidimensional array in PHP?

To delete all occurrences of an element from a multidimensional array in PHP, you can use a combination of array_walk_recursive() and unset() functions. Here's an example:

function deleteElement(&$array, $value) { array_walk_recursive($array, function (&$v, $k) use ($value) { if ($v === $value) { unset($v); } }); }

// Example multidimensional array $array = array( array("apple", "banana", "cherry"), array("apple", "banana", "mango"), array("apple", "grape", "cherry") );

// Deleting all occurrences of "banana" deleteElement($array, "banana");

// Print the updated array print_r($array);

Output:

Array ( [0] => Array ( [0] => apple [1] => cherry )

\[1\] => Array
    (
        \[0\] => apple
        \[2\] => mango
    )

\[2\] => Array
    (
        \[0\] => apple
        \[1\] => grape
        \[2\] => cherry
    )

)

In the above example, the deleteElement() function takes two parameters - the multidimensional array $array and the element to be deleted $value. Inside the function, array_walk_recursive() is used to iterate over each element in the array recursively. If the element matches the value to delete, it is unset using unset().

Note that unset() is called by reference using &$v to delete the element from the original array.

How to delete null values from an array in PHP?

To delete null values from an array in PHP, you can use the array_filter() function. It allows you to iterate through each element of the array and remove the ones that have a null value. Here's an example:

$array = [1, null, 3, null, 5];

$arrayWithoutNulls = array_filter($array, function($value) { return $value !== null; });

print_r($arrayWithoutNulls);

Output:

Array ( [0] => 1 [2] => 3 [4] => 5 )

In the above example, the array_filter() function is used to remove null values from the $array. The anonymous function inside array_filter() returns true for elements that are not null, thus leaving only non-null values in the resulting array $arrayWithoutNulls.

What is the method to remove an empty element from an array in PHP?

In PHP, you can use the array_filter() function to remove empty elements from an array. The array_filter() function accepts an array as the first parameter and a callback function as the second parameter. The callback function is used to define the filter condition, and any elements that satisfy the condition will be kept in the filtered array.

To remove empty elements, you can use the following code:

$array = array("element1", "", "element2", "", "element3");

// Define a callback function that removes empty elements function removeEmpty($value) { // Remove if the value is empty return !empty($value); }

// Apply array_filter() with the callback function $filteredArray = array_filter($array, "removeEmpty");

In this code, the array_filter() function filters the $array by applying the removeEmpty() function to each element. The removeEmpty() function checks if the value is empty using the empty() function, and returns true if it is not empty. The resulting filtered array will only contain non-empty elements, removing the empty elements like "" in this case.