Reputation: 1958
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.
Upvotes: 81
Views: 286609
Reputation: 1
$data = array(
0 => 'Key1',
1 => 'Key2'
);
$key = array_search('Key2', $data);
if ($key) {
echo 'Key is ' . $key;
} else {
echo 'Key not found';
}
Upvotes: 0
Reputation: 71
$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'));
Upvotes: 7
Reputation: 241
I prefer this
if(in_array($id,$user_access_arr) == false)
respective
if (in_array(search_value, array) == false)
// value is not in array
Upvotes: 24
Reputation: 5690
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
Upvotes: 3
Reputation: 5532
Simply
$os = array("Mac", "NT", "Irix", "Linux");
if (!in_array("BB", $os)) {
echo "BB is not found";
}
Upvotes: 110
Reputation: 71
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'));
}
Upvotes: 7
Reputation: 623
$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';
}
Upvotes: 3
Reputation: 5899
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'));
}
Upvotes: 3
Reputation: 96
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'));
}
Upvotes: 5