if I unset an array, would its elements be garbage collected or freed up assuming they are not referenced in anywhere else? what if I simply do $array =new array();
$array = array('a'=>1); //method 1 to clear array unset($array);
method 2 to clear an array
$array = array('a'=>1); //method 2 to clear array $array y = array();
As of PHP 7.3, PHP provides basic garbage collection information in user land PHP, via gc_status. The function returns: The number of garbage collection runs. The number of objects collected.
Basic example of empty array:$emptyArray = ( array ) null; var_dump( $emptyArray );
Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array.
Following simple code answers the question:
$a = array();
$a[0] = 'a1';
$a[1] = 'b2';
foreach($a as $v)
echo $v . '<br />';
//writes content of array
echo count($a) . '<br />';
//writes 2
$a = array(); //CLEAR ARRAY
foreach($a as $v)
echo $v . '<br />';
//writes nothing
echo count($a) . '<br />';
//writes 0
Check-out php < 5.3 garbage collection, do array values need to be set null or does setting the array = null orphan all its elements?, maybe that will help answer your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With