2016-11-15 81 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