2012-05-11 139 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