Checking to See If One Array'S Elements Are in Another Array in PHP

Checking to see if one array's elements are in another array in PHP

You can use array_intersect().

$result = !empty(array_intersect($people, $criminals));

PHP: Check if an array contains all array values from another array

Look at array_intersect().

$containsSearch = count(array_intersect($search_this, $all)) === count($search_this);

Or for associative array, look at array_intersect_assoc().

Or for recursive compare of sub-arrays, try:

<?php

namespace App\helpers;

class Common {
/**
* Recursively checks whether $actual parameter includes $expected.
*
* @param array|mixed $expected Expected value pattern.
* @param array|mixed $actual Real value.
* @return bool
*/
public static function intersectsDeep(&$expected, &$actual): bool {
if (is_array($expected) && is_array($actual)) {
foreach ($expected as $key => $value) {
if (!static::intersectsDeep($value, $actual[$key])) {
return false;
}
}
return true;
} elseif (is_array($expected) || is_array($actual)) {
return false;
}
return (string) $expected == (string) $actual;
}
}

Check if all array value exist in another array value

You can use array_diff()

array_diff — Computes the difference of arrays

Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.

<?php

$products = array("soap","milk","book");
$availableProducts = array("soap","tea","oil","milk","book");

$difference = array_diff($products,$availableProducts);

if(count($difference)==0){

echo "all products availabale";
}else{

echo implode(',',$difference) ." are not available";
}

Output:-

  1. https://eval.in/989587

  2. https://eval.in/989588

  3. https://eval.in/989593

  4. https://eval.in/989596

Check if an array's elements exists in another array in PHP

Since your expecting to push multiple items, you need to add another dimension inside:

$first = array("0"=> 111, "1"=>222, "2"=>333, "3"=> 444);
$second = array("0"=> 222, "1"=>234, "2"=> 456);

$final_array = array();
foreach($first as $f) {
$temp = array('id' => $f);
if(in_array($f, $second)){
$temp['check'] = 1;
} else {
$temp['check'] = 0;
}
$final_array[] = $temp;
}

Or just simply like this using a ternary:

$final_array = array();
foreach($first as $f) {
$final_array[] = array('id' => $f, 'check' => in_array($f, $second) ? 1 : 0);
}

What happens is that your values gets ovewritten each iteration:

$final_array['id'] = $f; // overwritten
$final_array['check'] = 1; // overwrriten

You need to push it with another dimension: $final_array[] = the array

PHP - Check if an array value is in another array

array_diff() does the opposite of what you want. It returns an array with the values of the first array that are not present in the other array(s).

You need array_intersect().

if (count(array_intersect($arr1, $arr2))) {
//at least one common value in both arrays
}

How to check if array has some elements same as another array and to pop those elements out of array

array_diff does exactly what you want.

$sourceArr = array(1,2,3,4,5);
$filterArr = array(2,4);
$result = array_diff($sourceArr, $filterArr);
var_dump($result);

Result:

array(3) {
[0]=>
int(1)
[2]=>
int(3)
[4]=>
int(5)
}

https://3v4l.org/IvmHH

Check to see if an array contains all elements of another array, including whether duplicates appear twice

Try creating this function:


function containsAll (target, toTest) {

const dictionary = {}

target.forEach(element => {
if (dictionary[element] === undefined) {
dictionary[element] = 1;
return;
}
dictionary[element]++;
});


toTest.forEach(element => {
if (dictionary[element] !== undefined)
dictionary[element]--;
})

for (let key in dictionary) {
if (dictionary[key] > 0) return false;
}

return true;

}

Then invoke it like this:

const arr1 = [1, 2, 2, 3, 5, 5, 6, 6]
const arr2 = [1, 2, 3, 5, 6, 7]


console.log(containsAll(arr1, arr2)) // returns false

How do I check if an array contains values from another array?

The issue is that your first array is actual an array of objects. And you are basically asking your PHP does this array of objects contain 3? This obviously is not a valid question. You need to convert the array of objects to a group of numbers.

From

array(3) {
[0]=>
object(stdClass)#8 (1) {
["interestId"]=>
string(1) "2"
}
[1]=>
object(stdClass)#6 (1) {
["interestId"]=>
string(1) "3"
}
[2]=>
object(stdClass)#9 (1) {
["interestId"]=>
string(1) "5"
}
}

To

array(3) {
[0]=>
string(1) "2"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
}

First of all, convert your user's interests to a simple array.

$userInterestsSimple = array();
foreach ($userInterests as $userInterest)
$userInterestsSimple[] = $userInterest->interestId;

Afterwards, your check with in_array() will work properly:

<option value="<?php echo $interest->interestID; ?>"
<?php
echo (in_array($interest->interestID, $userInterestsSimple)) ? ' selected="selected"' : '';
?>
>
<?php echo $interest->interestName; ?>
</option>

Checking if 2 arrays have at least 1 equal value

array_intersect()

returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved

$a = array(1, 2, 3, 4);
$b = array(4, 5, 6, 7);
$c = array_intersect($a, $b);
if (count($c) > 0) {
var_dump($c);
//there is at least one equal value
}

you get

array(1) {
[3]=>
int(4)
}


Related Topics



Leave a reply



Submit