2016-04-29 37 views
0

我試圖從的foreach刪除一些陣列 例如,對於「結果」數組一些數組:你如何刪除的foreach

$result= array ( 
    "1" => array(
    "nickname" => "Jow", 
    "Age"=> "19" 
    ), 
     "2" => array(
    "nickname" => "Rayan", 
    "Age"=> "25" 
    ), 
     "3" => array(
    "nickname" => "Jinx", 
    "Age"=> "21" 
    ), 
    ); 

我想暱稱刪除陣列1 mycode的:

foreach ($result as $client => $value){ 

if ($value["nickname"] == "Jow"){ 

    unset($result[$client]); 

    } 
echo $value["nickname"].'<p>'; 

} 

最後我要像RAYAN和金克斯

+0

退房'array_filter' – Ghost

+1

@RodrigoDuterte Oi!你在這裏。選舉近了。變化真的到來了嗎? – rhavendc

+0

你想要什麼? – suibber

回答

1

使用array_filter剩下的名字呢?

$result= array ( 
 
    "1" => array(
 
    "nickname" => "Jow", 
 
    "Age"=> "19" 
 
    ), 
 
     "2" => array(
 
    "nickname" => "Rayan", 
 
    "Age"=> "25" 
 
    ), 
 
     "3" => array(
 
    "nickname" => "Jinx", 
 
    "Age"=> "21" 
 
    ), 
 
); 
 

 
$arr = array_filter($result, 'filter_by_name'); 
 
var_dump($arr);exit; 
 

 
function filter_by_name($value){ 
 
\t if ($value['nickname']=='Jow') { 
 
\t \t return false; 
 
\t } else { 
 
\t \t return true; 
 
\t } 
 
}

+0

或者只是'return $ value ['nickname']!='Jow''' – Ghost

+0

@RodrigoDuterte是的,謝謝! – suibber

0

您正在尋找這樣的事情?

function removeByNickName($nickName, $array) 
{ 
    $temp = array(); 
    foreach($array as $index => $ar) 
    { 
     if($array['nickname'] != $nickName) array_push($temp, $ar); 
    } 
    return $temp; 
}