...

View Full Version : How to Clear an Array Completely?



dealmaker
12-17-2005, 09:54 PM
Hi,
I have an array variable that I use over and over. Can I clear it by just doing the following?

$myarray = array();

If the previous array has more elements than the current one, will it still have the old data at the end? I want to prevent this problem and I don't want to do a loop for efficiency consideration.

Many thanks.

Brandoe85
12-17-2005, 10:43 PM
You can just use unset() (http://us2.php.net/unset)

unset($array);

Good luck;

dealmaker
12-17-2005, 11:44 PM
After unset, I still have to do $myarray=array() before I do $myarray[]='hello', right?

You can just use unset() (http://us2.php.net/unset)

unset($array);

Good luck;

Element
12-18-2005, 12:08 AM
After unset, I still have to do $myarray=array() before I do $myarray[]='hello', right?

I think it does, and if I'm not mistaken you can do:


unset($array) ? $array=array() : $array=array();

So either way, when you use that it will reset array to $array=array();

Velox Letum
12-18-2005, 12:16 AM
You don't technically have to, but its good form.

Element
12-18-2005, 01:50 AM
You don't technically have to, but its good form.

Since Velox and me were talking about it I thought I'd share other ways to do this.




// Method 1
unset($array); // Unset $array.

// Method 2
$array=array(); // Reset $array to an empty array.

// Method 3
unset($array)?$array=array():$array=array(); // Unset and reset as an empty var, while still using method 2 encase unset() fails.

// Example 1
unset($array); // unset $array
$array[] = "foo"; // $array was unset but is not triggered as an array with the value "foo" as its first key.

// Example 2
$array=array(); // Reset array to an array
$array[] = "foo"; // Insert first key with the value "foo"

// Example 3
unset($array)?$array=array():$array=array(); // unset and reset array as an array.
$array[] = "foo" // Inset first key with the value "foo"

// Example 4
$array = array('foo'); // Reset $array as an array and insert first key with the value "foo"



The most simple would probably just be:


$array=array();

Which gives you a empty array to add to.

marek_mar
12-18-2005, 01:03 PM
unset() is not a function and cannot be used in if statement/ternarys/whatever.

missing-score
12-18-2005, 02:54 PM
unset() is not a function and cannot be used in if statement/ternarys/whatever.
Also, the whole point of ternary is that it is basically a shorthand if:else statement. Having the same thing on both sides of the colon : is completley defeating the point of what ternary is for.

Really, all you have to do is:



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


to get back where you started.

marek_mar
12-18-2005, 04:06 PM
unset() isn't really needed.
It's (logical) uses are when you wnat to delete a varible becouse you don't need it or becouse you want to make sure it's not available.
It only makes sense to unset variables in the global scope as variables which are in functions/methods get unset automaticly when the function/method stops executing.

Element
12-18-2005, 09:13 PM
unset() isn't really needed.
It's (logical) uses are when you wnat to delete a varible becouse you don't need it or becouse you want to make sure it's not available.
It only makes sense to unset variables in the global scope as variables which are in functions/methods get unset automaticly when the function/method stops executing.

As I had alredy said before unset isn't needed and you can just use:



// Example 2
$array=array(); // Reset array to an array
$array[] = "foo"; // Insert first key with the value "foo"


Which resets the array as an empty array, saves you two lines of code in the end and works just as well.

missing-score
12-18-2005, 09:51 PM
Actually, based on my tests, that doesn't do the same... It doesn't free the memory up that was used on the old variable. Unset specifically frees the memory up. I most cases this doesn't matter, but in the case of a very large set of data you should definatley use unset().

Element
12-18-2005, 09:57 PM
Actually, based on my tests, that doesn't do the same... It doesn't free the memory up that was used on the old variable. Unset specifically frees the memory up. I most cases this doesn't matter, but in the case of a very large set of data you should definatley use unset().

Indeed, if he is trying to destroy the data completely, he just wants to reset it to reuse. Either methods work, but what he asked originally works well enough.

If your using the array though and it contains variables that contain rich code it is probably more wise to use unset() just to clear the cache used on that array originaly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum