简体   繁体   English

从数组中排除多个值

[英]Excluding multiple values from the array

I have the following codes 我有以下代码

'delete' => function($url, $model) { 
                        $url = Url::to(['category/delete/'.$model->info_category_id]);
                        return ($model->info_category_id !== 11)?Html::a('<i class="icon-trash"></i>', $url, ['class'=>'black-txt tips del-confirm-subitems']):'';
                    },

which are about creating the delete function of a certain category in the backend. 这是关于在后端创建特定类别的删除功能。 As you see in the third line it excludes the category id number 11 from delete function. 如您在第三行中看到的,它从删除功能中排除了类别ID号11。 Beside category id number 11 I would also like to add category id number 15 from the database, however it leads to error when I insert 15 as following: ($model->info_category_id !== 11, 15) . 除了类别ID号11,我还想从数据库中添加类别ID号15,但是当我按如下方式插入15时,它会导致错误: ($model->info_category_id !== 11, 15)

I would appreciate if you could help me to insert category_id number 15 correctly to the codes. 如果您能帮助我在代码中正确插入category_id 15号,我将不胜感激。

Thanks in advance. 提前致谢。

Try the following: 请尝试以下操作:

return (!in_array($model->info_category_id,[11,15]))?Html::a('<i class="icon-trash"></i>', $url, ['class'=>'black-txt tips del-confirm-subitems']):'';

the way you are doing it is incorrect syntax for PHP. 这样做的方式是PHP的语法不正确。 The above example uses in_array function to determine whether the value of $model->info_category_id matches any value inside the provided array. 上面的示例使用in_array函数来确定$model->info_category_id的值是否与提供的数组内的任何值匹配。 Or you can try like this: 或者您可以尝试这样:

return ($model->info_category_id !== 11 && $model->info_category_id !== 15)?Html::a('<i class="icon-trash"></i>', $url, ['class'=>'black-txt tips del-confirm-subitems']):'';

It just checks against the values, separately. 它只是单独检查值。

You cannot do multiple comparisons like this: 您不能像这样进行多次比较:

$a !== $b, $c

You can however, do it like this: 但是,您可以这样做:

$a !== $b || $a !== $c

Or alternatively, you can use an array: 或者,您可以使用数组:

!in_array($a, [$b, $c])

You can have an array with the IDs you want to exclude, then include it in the scope of your closure and check if the ID exists in the array with in_array() : 您可以拥有一个要排除ID的数组,然后将其包括在闭包范围内,并使用in_array()检查ID是否存在于数组中:

$excluded = [11, 15];
'delete' => function($url, $model) use ($excluded) { 
     $url = Url::to(['category/delete/'.$model->info_category_id]);
     return (!in_array($model->info_category_id, $excluded))?Html::a('<i class="icon-trash"></i>', $url, ['class'=>'black-txt tips del-confirm-subitems']):'';
 },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM