如何删除所有变量和存储数组以及一些php变量和循环?

How to delete all variable and store array and some php variable and loop while ?

This is my code for delete all variable and store array objResult and $c variable and loop while until complete.

But when i test code. it's will be store only $c, How to store array objResult too.

And my code not loop while too. how can i do for store $c and array objResult and loop while until complete.

<?PHP
include("connect.php");
$strSQL = "SELECT * FROM table WHERE name = ''";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery))
{
    $a = "111";
    $b = "122211";
    $c = "333";
    $d = "444";
    $defined_variables = get_defined_vars();
    $variables2keep = array("objResult" , "c");  // not clear $d and array objResult//
    foreach($defined_variables as $variable => $value) 
    if(!in_array($variable, $variables2keep)) unset($$variable);

    echo $objResult["id"];
}
?>

Change your code to the below:

<?php
include("connect.php");
$strSQL = "SELECT * FROM table WHERE name = ''";
$objQuery = mysql_query($strSQL);
while($objResult = mysql_fetch_array($objQuery)) {
    $a = "111";
    $b = "122211";
    $c = "333";
    $d = "444";
    $variables2keep = array("objResult" , "c", "strSQL", "objQuery");  // not clear $c and array objResult, strSQL and objQuery must remain for while statement to continue through record set
    $defined_variables = get_defined_vars();
    foreach($defined_variables as $variable => $value) {
        if(!in_array($variable, $variables2keep)) unset($$variable);
    }
}
unset($variable);
unset($value);
unset($strSQL);
unset($objQuery);
unset($defined_variables);

print_r(get_defined_vars());
?>

Setting $variables2keep above the get_defined_vars will remove this. As $defined_variables, $variable, $value has been set after $defined_variables, you need to unset these too.

You can then get the variables again by: (after the unset)

print_r(get_defined_vars());

You will see you only have the two variables you want