从类方法中取消设置php中的变量

<?php
namespace foo;
class bar{
  public static function runner( $data )
  {

     extract ($data);

     foreach( $elements as $element )
     {
        $varsToWipe = array_keys($element);
        extract( $element );

        /*
            bunch of code using the variables extracted from $element, eg   if( isset($runnerSpeed) )
        */

        //now i want to be able to unset all the elements set from the extract within the foreach loop
        foreach( $varsToWipe as $var )
        {
            //but what goes here in the unset function?
            \unset(  );
        }
     }

  }
}

How can I unset the variables extracted from within the foreach loop in the runner method?

The contents of $data can vary and the vars need to be unset so as not used again on the next loop iteration. I know i can ref the array itself but would be quicker to write if this could work...

Thanks, John

A simpler foreach example:

$array = range(0,6);
$i = 0;
foreach( $array as $a )
{
   echo $i.' ';
   if( $i == 0 )
  {
    $egg = true;
  }
  ++$i;
  if( isset($egg) )
  {
     echo 'eggs ';
  }
}

will print

0 eggs 1 eggs 2 eggs 3 eggs 4 eggs 5 eggs 6 eggs

You could then add a removal of $egg from the globals as this is where is sits:

$array = range(0,6);

$i = 0;

foreach( $array as $a )
{
   echo $i.' ';
   if( $i == 0 )
  {
    $egg = true;
  }
  else
  {
     unset( $GLOBALS['egg'] );
  }
  ++$i;
  if( isset($egg) )
  {
     echo 'eggs ';
  }
}

Now it would print:

0 eggs 1 2 3 4 5 6

But what do you unset when you are within a class method, where is the variable actually stored?

Don't use the extract() function in the first place and simply iterate over the given array.

<?php

class Foo {

  public function runner($data) {
    foreach ($data as $delta => $element) {
      if ($element === "something") {
        // do something
      }
    }
  }

}

Of course you could unset something inside the array now, but why would you want to? I can't see a reason why because the array isn't passed by reference and isn't used after the loop so it will go out of scope and is simply collected by the garbage collector.

In case of nested arrays:

foreach ($data as $delta => $element) {
  if (is_array($element)) {
    foreach ($element as $deltaInner => $elementInner) {
      // ...

Pretty late now but for reference.

You can unset() them directly with:

unset($$var);

What extract does is equivalent to:

foreach($arr as $key => $value) {
    $$key = $value;
}

So to unset you do:

foreach(array_keys($arr) as $key) {
    unset($$key);
}

But like mentioned, you don't need to unset() them as they are deleted when the function completes as they will become out of scope.

Also, extract() is totally fine as it (most likely) does not copy the data values, it just creates new symbols for each array key. PHP internally will only copy the values if you actually modify it ie. copy-on-write with reference counting.

You can off course test this using memory_get_usage() before and after you do extract() on a large array.