2012-05-11 173 views
1

我正在通過一系列國家進行搜索,看看是否有匹配的英國,如果是的話,我把它拉出來,放在我的下拉列表頂部。PHP,從數組中刪除?

foreach($this->registry->stockistCountries as $value) 
{ 
    if($value['country'] == 'UK'){ 
     $buffer .= '<option>UK</option>'; 
     $buffer .= '<option disabled>------------------</option>'; 
    } 

} 

我想知道,如果英國被發現,有沒有辦法將它從數組$ this-> registry-> stockistCountries?

感謝

+1

你試過'unset()'? – Leri

回答

2

你的循環更改爲:

foreach($this->registry->stockistCountries as $i => $value) 
{ 
    if($value['country'] == 'UK'){ 
     $buffer .= '<option>UK</option>'; 
     $buffer .= '<option disabled>------------------</option>'; 
     unset($this->registry->stockistCountries[$i]); 
    } 

} 
1

只是改變你的foreach -loop也拿到了鑰匙,然後只用unset()

foreach($this->registry->stockistCountries as $key => $value){ 
            // add this ^^^^^^^^ 
    // do something 
    if(/* whatever */){ 
    unset($this->registry->stockistCountries[$key]); 
    } 
} 
0

喜歡的東西 @unset($this->registry->stocklist['country']['UK']);

+0

如果這種情況下你不需要運行foreach() – Vasisualiy

0
$found = array_search('UK', $this->registry->stockistCountries); //search for UK 


unset($this->registry->stockistCountries[$found]); // Remove from array