Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:31:27+00:00 2026-06-02T09:31:27+00:00

Which is more efficient for clearing all values in an array? The first one

  • 0

Which is more efficient for clearing all values in an array? The first one would require me to use that function each time in the loop of the second example.

foreach ($array as $i => $value) {
    unset($array[$i]);
}

Or this

foreach($blah_blah as $blah) {
    $foo = array();
    //do something
    $foo = null;
}

I don’t want to use unset() because that deletes the variable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:31:28+00:00Added an answer on June 2, 2026 at 9:31 am

    Like Zack said in the comments below you are able to simply re-instantiate it using

    $foo = array(); // $foo is still here
    

    If you want something more powerful use unset since it also will clear $foo from the symbol table, if you need the array later on just instantiate it again.

    unset($foo); // $foo is gone
    $foo = array(); // $foo is here again
    

    If we are talking about very large tables I’d probably recommend

    $foo = null; 
    unset($foo); 
    

    since that also would clear the memory a bit better. That behavior (GC) is however not very constant and may change over PHP versions. Bear in mind that re-instantiating a structure is not the same as emptying it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering which is more efficient, to store temporary data (related to that
Which of these queries is more efficient, and would a modern DBMS (like SQL
Possible Duplicates: Implementing a matrix, which is more efficient - using an Array of
The question is simply between $(body).click(function(e){}); vs $(document).click(function(e){}); which is more efficient or advisable?
This <cfif len(x)> Or <cfif x NEQ > Which one is more efficient and
which is more efficient/better code. to use is object and then unbox if it
which is more efficient (when managing over 100K records): A. Mysql SELECT * FROM
Which set of selectors is more efficient? 1 $('#parent_element span.class1').do_something1(); $('#parent_element span.class2').do_something2(); $('#parent_element span.class3').do_something3();
My real Question is which of these is more efficient in terms of loading
Is there any time in which a reference type is more efficient than a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.