Re: Unsetting Array Element

From: Date: Wed, 13 Nov 2002 15:25:52 +0000
Subject: Re: Unsetting Array Element
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-124274@lists.php.net to get a copy of this message
The short answer to your question is yes. Here's a little demo script: <?php $array = array(
        "title" => "Title of Document",
        "content" => "Ten paragraphs of text in here...",
        "spork" => 12345,
        "blahblah" => "Yes we have no bananas",
        "fnoop" => 555566666
); echo '# elements=', count($array), ', serialized length=', strlen(serialize($array)), "\n"; unset($array['content']); echo '# elements=', count($array), ', serialized length=', strlen(serialize($array)), "\n"; foreach ($array as $k=>$v) {
        echo "$k=>$v\n";
} ?> and here's the output: # elements=5, serialized length=187 # elements=4, serialized length=132 title=>Title of Document spork=>12345 blahblah=>Yes we have no bananas fnoop=>555566666 The element count & total size of the array go down, and the foreach doesn't show a 'hole' in the array. The array length determined by serialize() (which should be larger that the in-memory length) goes down by 55, equal to strlen('content') + strlen("Ten paragraphs of text in here...") + 15 bytes, which are presumably serialize()'s overhead for saving array element types & positions. -steve At 2:11 AM -0500 11/13/02, Monty wrote:
Does unsetting an array element make the array smaller? For example, if I have two elements in an array...
    $array = ("title" => "Title of Document",
              "content" => "Ten paragraphs of text in here..." );
...then issue this command...
    unset ($array['content']);
...will this make $array smaller and more efficient if I then pass it on to a function for processing? Thanks!
-- +------------------------------------------------------------------------+ | Steve Edberg sbedberg@ucdavis.edu | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | +------------------------------------------------------------------------+ | SETI@Home: 1001 Work units on 23 oct 2002 | | 3.152 years CPU time, 3.142 years SETI user... and STILL no aliens... | +------------------------------------------------------------------------+

« previous php.general (#124274) next »