dongpaipu8394 2015-07-06 10:30
浏览 51
已采纳

如何删除所有变量和存储数组以及一些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"];
}
?>
  • 写回答

1条回答 默认 最新

  • dongqiao8502 2015-07-06 10:42
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用