in_array multiple values
Intersect the targets with the haystack and make sure the intersection count is equal to the target's count:
$haystack = array(...);
$target = array('foo', 'bar');
if(count(array_intersect($haystack, $target)) == count($target)){
// all of $target is in $haystack
}
Note that you only need to verify the size of the resulting intersection is the same size as the array of target values to say that $haystack
is a superset of $target
.
To verify that at least one value in $target
is also in $haystack
, you can do this check:
if(count(array_intersect($haystack, $target)) > 0){
// at least one of $target is in $haystack
}
check multiple values using in_array in PHP
You need to use array_intersect()
to see what values are in both arrays. in_array()
checks to see if one value exists in an array so that won't work for you (unless you use a loop to iterate through your $tempRecordTypes
array and compare it to the $allRecordTypes
array).
$RecordType = array_intersect($tempRecordTypes,$allRecordTypes);
PHP if in_array for multiple values
Try and see if this is helpful:
if(array_intersect($result, array('abc', '123', 'def'))) {
$this->login_session($user);
}
How to check multiple values in an array with AND , OR operators
Your problem is happening because you only checking 1 permission as you use return
in the foreach
.
You should do it like this:
public static function checkPermissions($permissionLevels = []){
$userPermission =[1,4,5,6];
foreach($permissionLevels as $permission){
if (!in_array($permission,$userPermission))
return false; // if one is missing decline
}
return true; // if got here mean all found
}
You can also use array-intersect as:
function checkPermissions($permissionLevels = []){
$userPermission =[1,4,5,6];
$permissionLevels = array_unique($permissionLevels);
return count(array_intersect($permissionLevels, $userPermission)) == count($permissionLevels));
}
Do notice to use array_unique
if using the array_intersect
as you can have duplicate - consider case where checkPermissions([4,4]);
Multiple values in array excluding specified key
Unset id
index and then do search:
function in_array_any($needles, $haystack) {
unset($haystack['id']);
return !array_diff($needles, $haystack);
}
https://3v4l.org/PTjOS
PHP in_array isn't finding multiple values
You're using in_array wrong. It doesn't treat the "needle" specially. It looks for exact copies of the needle in the haystack:
php > $foo = array(1,2,3);
php > var_dump(in_array(array(1,2),$foo));
bool(false)
php > $bar = array(array(1,2),array(2,3), array(3,4));
php > var_dump(in_array(array(1,2), $bar));
bool(true)
Therefore you can't use a single in_array call to check for the existence of MULTIPLE values.
Check if one of multiple values exists in multidimensional array
Function to check existence of value in multidimensional array.
Function return true or false, you can use it in general.
function search_in_array($value, $array) {
if(in_array($value, $array)) {
return true;
}
foreach($array as $item) {
if(is_array($item) && search_in_array($value, $item))
return true;
}
return false;
}
Working example
--------- edit -----------
Based on your comment, and an example of an array
function check_in_array($value, $array, $key){
foreach($array as $item){
if($item[$key] == $value)
return true;
}
return false;
}
And then call it like this check_in_array('orange', $array, 'FacetValueName');
Or this
check_in_array('orange', $array, 'FacetValueName') && check_in_array('Orange', $array, 'FacetValueName') && check_in_array('dark orange', $array, 'FacetValueName')
to check multiple values at once.
Working example
check multiple values exists php array
Use array_intersect
count(array_intersect($permission, $userRoles));
PHP Checking multiple values in an array - the right way?
You can do it something like this.
Solution 1:
Here we are using array_intersect
for checking common values between two arrays.
<?php
ini_set('display_errors', 1);
$valuecheck = false;
$daafids=array(96,97,99,122,123,124,125,126,127,128,129,130,131,132,133);
if(count(array_intersect($prodcat, $daafids))>0)
{
$valuecheck=true;
}
Here we are using in_array
to search that particular element in the array.
Solution 2:
<?php
ini_set('display_errors', 1);
$valuecheck = false;
$daafids=array(96,97,99,122,123,124,125,126,127,128,129,130,131,132,133);
foreach ($prodcat as $daafid) {
if(in_array($daafid,$daafids)){
$valuecheck=true;
break;
}
}
if ($valuecheck == true) {
echo $text_true;
}
check multiple values from checkbox in PHP
<?php
if(!empty($_POST['check_list'])){
if(
has_values(Array(2,3,6), $_POST['check_list'])
){
//Do what you need to do
}else if(
has_values(Array(2,3), $_POST['check_list'])
){
//Do what you need to do
}else if(
has_values(Array(2), $_POST['check_list'])
){
//Do what you need to do
}
}else{
//no checkboxes have been set
}
function has_values($testValues, $arrValues){
/*
testValues is a 1 dimensional array (needles)
arrValues is a 1 dimensional array that takes the array of set checkboxs ( haystack )
*/
foreach ($testValues as $key => $value) {
if(!in_array($value, $arrValues)){
return false;
}
}
return true;
}
Developer notes
in_array is not type dependent.
if a checkbox is not checked the $_POST['check_list'] index will changes
Related Topics
How to Access Dynamic Variable Names in Twig
"Vertical" Regex Matching in an Ascii "Image"
In PHP, What Does "≪≪≪" Represent
Insert - Number of Bind Variables Doesn't Match Number of Fields in Prepared Statement
PHP Curl, Extract an Xml Response
How to Embed Youtube Videos in PHP
How to Check If the Current Date/Time Is Past a Set Date/Time
PHP How to Start an External Program Running - Having Trouble With System and Exec
Checking If a Variable Is an Integer in PHP
MySQLi_Fetch_Array While Loop Columns
Is There Any Particular Difference Between Intval and Casting to Int - '(Int) X'
Submit an HTML Form With Empty Checkboxes
How to Re-Index the Values of an Array in PHP
How to Best Store User Information and User Login and Password
MySQL Server Has Gone Away - in Exactly 60 Seconds