1

NOTE: It seems I was wrong about what was happening, and that there is no issue using $a = array();. This is since all assignments to arrays is by copy. (I had thought there were some accesses by reference that were causing problems - but that was just a typo. I've added some details to an answer below.

I've got some PHP that looks like this:

$myArray = array();

function useArray() {
  global $myArray;
  // ... do something with myArray ...
}

function clearArray() {
  global $myArray;
  // ... Somehow clear the global array ...
}

I know this sucks from a design viewpoint, but it's required to work around some third-party code that I can't change...

My question is what can I put in the clearArray function to make it work? The usual advice of using $myArray=array(); or unset($myArray); don't work since they only change the local version, not the global version. I guess I could loop over the keys in the array and unset each in turn - like this:

function clearArray() {
  global $myArray;
  foreach($key in array_keys($myArray) ) {
     unset( $myArray[$key] );
  }
}

But that seems hacky and unclear. Is there a better solution?

5
  • Does that 'hacky' version work? Commented Aug 13, 2013 at 9:06
  • 1
    $myArray = [] works -- do you get different results? Commented Aug 13, 2013 at 9:07
  • 4
    My tests show that $myArray = array(); works fine - what issues are you having? Commented Aug 13, 2013 at 9:10
  • 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?][1], maybe that will help answer your question. [1]: stackoverflow.com/questions/4826066/… Commented Aug 13, 2013 at 9:12
  • @SmokeyPHP - you're absolutely right. Some confusion about on my part about array assignment vs object assignment, plus a missing "global" in part of my code caused me to jump to the wrong conclusions. Commented Aug 14, 2013 at 1:21

5 Answers 5

8

Don't forget there're two ways to access a global variable:

function clearArray() {
    unset($GLOBALS['myArray']);
}
Sign up to request clarification or add additional context in comments.

Comments

1

The usual advice of using $myArray=array(); or unset($myArray); does obviously work.

1 Comment

unset ($myArray) may be obvious, but it most certainly does not work.
0

You can pass by reference:

$a = array ('one', 'two', 'three');

print_r ($a);
fone ($a);
print_r ($a);
ftwo ($a);
print_r ($a);

function fone ($a) 
{
   $a = array ();
}

function ftwo (&$a)
{
   $a = array ();
}

Result:

Array
(
    [0] => one
    [1] => two
    [2] => three
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)
Array
(
)

Comments

0

PHP's array assignment is by copy (while object assignment is by reference). This means many of the holes you think exist, simply do not exist.

Here's a simple example:

$a = array("A");
$b = &$a;
$c = $a;


function clear1() {
  global $a;
  $a = array();
}

clear1();
print(json_encode(array($a,$b,$c))."\n"); 

This outputs [[],[],["A"]]. The same result is obtained using the clear from your post.

function clear2(){
  global $a;
  foreach(array_keys($a) as $key) {
    unset( $a[$key] );
  }
}

The reason $c is not cleared while $b is, is that $c is a deep copy of $a (the default for array assignment), while $c is a reference to $a. The key is that arrays are values, not references.

Thus it seems likely that your error is either a missing global or a misunderstanding of how array/object assignment works. (And in reality was both.)

Comments

0

I know this is an old thread, but I came upon it trying to solve the same problem as the original commenter. Ultimately the actual answer is in bits and pieces of accepted answers, but never stated explicitly.

This is the link that ultimately gave me the correct answer: http://www.nusphere.com/kb/phpmanual/function.unset.htm?

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

If you would like to unset() a global variable inside of a function, you can use the $GLOBALS array to do so.

The actual answer should look like this:

function clearArray() {
  global $myArray;
  unset($GLOBALS['myArray']);
  $myArray =array();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.