For cleared or unset php arrays, are elements garbage collected?

后端 未结 3 1465
执笔经年
执笔经年 2021-01-13 06:26

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();

相关标签:
3条回答
  • 2021-01-13 06:35

    The following will modify the array itself and leave it empty:

    array_splice($myArray, 0);
    

    And the Splice Documentation

    0 讨论(0)
  • 2021-01-13 06:45

    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.

    0 讨论(0)
  • 2021-01-13 07:02

    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
    
    0 讨论(0)
提交回复
热议问题