77

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_array function of PHP. I am trying to check it using (!in_array) but I did not got result.

Dharman
  • 30,962
  • 25
  • 85
  • 135
s4suryapal
  • 1,880
  • 1
  • 18
  • 26

9 Answers9

102

Simply

$os = array("Mac", "NT", "Irix", "Linux");
if (!in_array("BB", $os)) {
    echo "BB is not found";
}
Elyor
  • 5,396
  • 8
  • 48
  • 76
21

I prefer this

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

respective

if (in_array(search_value, array) == false) 
// value is not in array 
Tomas Kaidl
  • 211
  • 2
  • 4
  • What is for OP? – Tomas Kaidl Sep 14 '17 at 09:23
  • Excuse me, that's the original poster so in this case the person asking a question. It helps if answers are explained.https://stackoverflow.com/help/how-to-answer – Edwin Sep 14 '17 at 09:32
  • Yeah, but more than answer for OP are answers here for people searching similar problems, I guess. > How to check not in array element in php – Tomas Kaidl Sep 14 '17 at 09:40
7
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'));
}
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Shruti
  • 71
  • 1
  • 3
7
$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'));
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
Umesh Pimpale
  • 71
  • 1
  • 2
5

Try with array_intersect method

$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'));
}
Ajanth
  • 96
  • 2
  • 9
3

I think everything that you need is 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'));
            }
ackuser
  • 5,681
  • 5
  • 40
  • 48
3
$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';
}
aish
  • 623
  • 1
  • 7
  • 11
3

you can check using php in_array() built in function

<?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.

$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

Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
0
$data = array(
0 => 'Key1',
1 => 'Key2'
);

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

if ($key) {
 echo 'Key is ' . $key;
} else {
 echo 'Key not found';
}
  • 2
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – jasie Mar 03 '21 at 09:42