Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to Clear an Array Completely?

    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.

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You can just use unset()

    unset($array);

    Good luck;

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    After unset, I still have to do $myarray=array() before I do $myarray[]='hello', right?
    Quote Originally Posted by Brandoe85
    You can just use unset()

    unset($array);

    Good luck;

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by dealmaker
    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:

    PHP Code:
    unset($array) ? $array=array() : $array=array(); 
    So either way, when you use that it will reset array to $array=array();

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You don't technically have to, but its good form.
    "$question = ( to() ) ? be() : ~be();"

  • #6
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Velox Letum
    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.

    PHP Code:
     
    // 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:

    PHP Code:
    $array=array(); 
    Which gives you a empty array to add to.
    Last edited by Element; 12-18-2005 at 01:52 AM.

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    unset() is not a function and cannot be used in if statement/ternarys/whatever.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #8
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    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:

    PHP Code:
    unset( $array );
    $array = array(); 
    to get back where you started.

  • #9
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #10
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by marek_mar
    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:

    PHP Code:
    // 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.

  • #11
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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().

  • #12
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by missing-score
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •