2

我在一系列国家中搜索以查看是否与英国匹配,如果是,我将其拉出并将其放在下拉列表的顶部。

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

}

我想知道,如果找到 UK,有没有办法从数组 $this->registry->stockistCountries 中删除它?

谢谢

4

4 回答 4

3

将循环更改为:

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

}
于 2012-05-11T12:01:51.177 回答
1

只需更改您的foreach-loop 以获取密钥,然后使用unset()

foreach($this->registry->stockistCountries as $key => $value){
                                  // add this ^^^^^^^^
  // do something
  if(/* whatever */){
    unset($this->registry->stockistCountries[$key]);
  }
}
于 2012-05-11T12:01:07.973 回答
0

就像是 @unset($this->registry->stocklist['country']['UK']);

于 2012-05-11T12:02:53.437 回答
0
$found = array_search('UK', $this->registry->stockistCountries); //search for UK


unset($this->registry->stockistCountries[$found]); // Remove from array
于 2012-05-11T12:05:31.953 回答