排除来自foreach循环的值

排除来自foreach循环的值

本文介绍了排除来自foreach循环的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码..我知道这可能都是错误的,但我没有处理foreach循环之前。

  $ last_names = regapiGetLastNames(NULL,-1); 
foreach($ last_names as $ name => $ last_name_id)
$ exclude = array('11196','11195','11198','11197');
if(!in_array($ name-> last_name_id,$ exclude)):
print'< option value =''。$ last_name_id。'>'。$ name。'< /选项>';


解决方案

如果ID是数组值,那么您也可以使用来筛选它们:

$ $ $ $ $ $ last_names = regapiGetLastNames(NULL, -1);

$ exclude = array('11196','11195','11198','11197');
$ last_names = array_diff($ last_names,$ exclude);

foreach($ last_names as $ name => $ last_name_id){
print'< option value ='。$ last_name_id。>'。$ name。'< ; /选项>';
}


I have the following code.. and I know it's probably all wrong, but I haven't dealt with foreach loops before.

$last_names = regapiGetLastNames( NULL, -1 );
foreach ($last_names as $name => $last_name_id)
    $exclude = array('11196','11195','11198','11197');
    if(!in_array($name->last_name_id, $exclude)):
    print '<option value="'.$last_name_id.'">'.$name.'</option>';

Obviously its going wrong somewhere, any help pls?

解决方案

If the IDs are array values, then you can also use array_diff to filter them:

$last_names = regapiGetLastNames( NULL, -1 );

$exclude = array('11196','11195','11198','11197');
$last_names = array_diff($last_names, $exclude);

foreach ($last_names as $name => $last_name_id) {
    print '<option value="'.$last_name_id.'">'.$name.'</option>';
}

这篇关于排除来自foreach循环的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:32