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

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大