php 如何检查不在php中的数组元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22316925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:49:49  来源:igfitidea点击:

How to check not in array element in php

phparrays

提问by s4suryapal

I am trying to check if an element is not in array than want to redirect page: My code is as below:

我试图检查一个元素是否不在数组中而不是想要重定向页面:我的代码如下:

$id = $access_data['Privilege']['id']; 

if(!in_array($id,$user_access_arr))
{
    $this->Session->setFlash(__('Access Denied! You are not eligible to access this.'), 'flash_custom_success');
    return $this->redirect(array('controller'=>'Dashboard','action'=>'index'));
}

I am confused how to check if element is not in array. As we can check element exist or not in array using in_arrayfunction of php. I am trying to check it using (!in_array)but I did not got result.

我很困惑如何检查元素是否不在数组中。因为我们可以使用in_arrayphp 的函数检查元素是否存在于数组中。我正在尝试使用它进行检查,(!in_array)但没有得到结果。

Please help.

请帮忙。

回答by Elyor

Simply

简单地

$os = array("Mac", "NT", "Irix", "Linux");
if (!in_array("BB", $os)) {
    echo "BB is not found";
}

回答by Tomas Kaidl

I prefer this

我更喜欢这个

if(in_array($id,$user_access_arr) == false)

respective

各自

if (in_array(search_value, array) == false) 
// value is not in array 

回答by Shruti

if (in_array($id,$user_access_arr)==0)
{
    $this->Session->setFlash(__('Access Denied! You are not eligible to access this.'), 'flash_custom_success');
    return $this->redirect(array('controller'=>'Dashboard','action'=>'index'));
}

回答by Umesh Pimpale

$id = $access_data['Privilege']['id']; 

if(!in_array($id,$user_access_arr));
    $user_access_arr[] = $id;
    $this->Session->setFlash(__('Access Denied! You are not eligible to access this.'), 'flash_custom_success');
    return $this->redirect(array('controller'=>'Dashboard','action'=>'index'));

回答by Ajanth

Try with array_intersectmethod

尝试使用array_intersect方法

$id = $access_data['Privilege']['id']; 

if(count(array_intersect($id,$user_access_arr)) == 0){

$this->Session->setFlash(__('Access Denied! You are not eligible to access this.'), 'flash_custom_success');
return $this->redirect(array('controller'=>'Dashboard','action'=>'index'));
}

回答by ackuser

I think everything that you need is array_key_exists:

我认为你需要的一切都是array_key_exists

if (!array_key_exists('id', $access_data['Privilege'])) {
                $this->Session->setFlash(__('Access Denied! You are not eligible to access this.'), 'flash_custom_success');
                return $this->redirect(array('controller' => 'Dashboard', 'action' => 'index'));
            }

回答by aish

$array1 = "Orange";
$array2 = array("Apple","Grapes","Orange","Pineapple");
if(in_array($array1,$array2)){
    echo $array1.' exists in array2';
}else{
    echo $array1.'does not exists in array2';
}

回答by Shafiqul Islam

you can check using php in_array() built in function

您可以使用 php in_array() 内置函数进行检查

<?php
  $os = array("Mac", "NT", "Irix", "Linux");
  if (in_array("Irix", $os)) {
    echo "Got Irix";
 }
 if (in_array("mac", $os)) {
  echo "Got mac";
}
?>

and you can also check using this

你也可以使用这个检查

<?php
  $search_array = array('first' => 1, 'second' => 4);
  if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
   }
   ?>

in_array() is fine if you're only checking but if you need to check that a value exists and return the associated key, array_search is a better option.

in_array() 如果您只是检查就可以,但如果您需要检查值是否存在并返回关联的键,则 array_search 是更好的选择。

$data = array(
0 => 'Key1',
1 => 'Key2'
);

$key = array_search('Key2', $data);

if ($key) {
 echo 'Key is ' . $key;
} else {
 echo 'Key not found';
}

for more details http://php.net/manual/en/function.in-array.php

更多详情 http://php.net/manual/en/function.in-array.php