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.
The Array. isArray() method checks whether the passed variable is an Array object. It returns a true boolean value if the variable is an array and false if it is not.
contains() method in Java is used to check whether or not a list contains a specific element. To check if an element is in an array, we first need to convert the array into an ArrayList using the asList() method and then apply the same contains() method to it.
isArray(variableName) method to check if a variable is an array. The Array. isArray(variableName) returns true if the variableName is an array. Otherwise, it returns false .
The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.
Simply
$os = array("Mac", "NT", "Irix", "Linux"); if (!in_array("BB", $os)) { echo "BB is not found"; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With