How to delete an empty array in php?

Member

by brandy , in category: PHP General , 6 months ago

How to delete an empty array in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by samara , 6 months ago

@brandy 

In PHP, you cannot delete an empty array directly because it does not occupy any memory space. However, you can assign a new value to the variable holding the array to remove the reference to the empty array. Here's an example:

1
2
3
4
5
6
7
8
9
$array = []; // empty array

// unset the variable to remove the reference to the empty array
unset($array);

// Alternatively, you can assign a new value to the variable
$array = null;

// Now, the empty array is no longer accessible


Both unset($array) and $array = null will achieve the desired result of removing the empty array.