cakephp 如何检查不在数组元素中

tvmytwxo  于 2022-11-12  发布在  PHP
关注(0)|答案(9)|浏览(135)

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

$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'));
}

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

kxxlusnw

kxxlusnw1#

简单地

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

jk9hmnmh2#

我更喜欢这个

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

各自的

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

x6h2sr283#

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'));
}
ipakzgxi

ipakzgxi4#

$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'));
pvabu6sv

pvabu6sv5#

尝试使用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'));
}
watbbzwu

watbbzwu6#

我认为您需要的所有内容都是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'));
            }
i2loujxw

i2loujxw7#

$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';
}
wb1gzix0

wb1gzix08#

你可以使用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";
}
?>

你也可以用这个

<?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()是合适的,但如果需要检查值是否存在并返回相关的键,array_search是更好的选择。

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

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

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

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

7jmck4yq

7jmck4yq9#

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

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

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

相关问题