【问题标题】:php unset not workingphp未设置不起作用
【发布时间】:2017-03-29 06:18:13
【问题描述】:

好的,不知道为什么这不起作用

$info 包含一个数组,有 3 个 user_pass 副本应该全部移除,前两个被移除,但第三​​个没有。

有什么想法吗?

if($phoneDetails['show_passwd'] == '0') {
    for($i = 0; $i < count($info); $i++) {
        if($info[$i]['header']['tag'] == 'user_pass') {
            unset($info[$i]);
        }elseif($info[$i]['header']['tag'] == 'http_pass') {
            unset($info[$i]);
        }
    }
}

【问题讨论】:

  • 请提供$info数组,

标签: php unset


【解决方案1】:

您的代码依赖于数组的索引从0N-1 的事实。这真的是你的情况吗?如果您将for 循环替换为foreach 会怎样:

if ($phoneDetails['show_passwd'] == '0') {
  foreach ($info as $i => $v) {  
    if ($info[$i]['header']['tag'] == 'user_pass']) {
      unset($info[$i]);
    } else if ($info[$i]['header']['tag'] == 'http_pass') {
      unset($info[$i]);
    }
  }
}

【讨论】:

    【解决方案2】:

    您给出的代码相同,但重构得更多。顺便说一句,我没有得到你真正需要的东西

    if($phoneDetails['show_passwd'] == '0') { //or if(!$phoneDetails['show_passwd'])
        $i = 0;
        for($i; $i < count($info); $i++) { 
            $tag = !empty($info[$i]['header']['tag']) ? $info[$i]['header']['tag'] : '';
            if ($tag == 'user_pass' || $tag == 'http_pass') {
                unset($info[$i]);
            }
        }
    }
    

    【讨论】:

    • aah,可能是因为 for 循环中的 count($info)。 $i
    猜你喜欢
    • 2016-07-06
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    相关资源
    最近更新 更多