2016-11-15 83 views
0

OK,不知道为什么,这是行不通的PHP未设置不工作

$信息包含数组,有user_pass 3份应全部拆除,前两个被删除,但第三​​个是没有的。

任何想法?

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]); 
     } 
    } 
} 
+0

请提供'$ info'阵列, –

回答

0

你的代码依赖于一个事实,即数组的索引去从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]); 
    } 
    } 
} 
0

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

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]); 
     } 
    } 
} 
+0

AAH,可能是因为计数($信息)内的循环。 $ i