How to check not in array element in php

s4suryapal picture s4suryapal · Mar 11, 2014 · Viewed 149.3k times · Source

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.

Please help.

Answer

Elyor picture Elyor · Sep 8, 2016

Simply

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