PHP: Check If an Array Contains All Array Values from Another Array

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 contains another array with PHP

You can try

$GroupOfEight = array(
array(0,1,3,2,4,5,7,6),
array(4,5,6,7,15,12,13,14),
array(12,13,15,14,8,9,11,10),
array(2,6,14,10,3,7,15,11),
array(1,3,5,7,13,15,9,11),
array(0,4,12,8,1,5,13,9),
array(0,1,3,2,8,9,11,10));

$stackArray = array(0,4,12,1,9,8,5,13,9,2,5,2,10);

function searcheight($stackArray, $GroupOfEight) {
$list = array();
for($i = 0; $i < count($GroupOfEight); $i ++) {
$intercept = array_intersect($GroupOfEight[$i], $stackArray);
$len = count($intercept);
if ($len % 4 == 0) {
$list[$i] = $len;
}
}
arsort($list);
if (empty($list))
return - 1;
return key($list);
}
echo searcheight($stackArray, $GroupOfEight);

Output

5

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));

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 an array contains all elements of another array

Given your sample arrays, the output of this will be:

> 0

In case you HAD to have only one number output, this should do that:

<?php
//Check if stackArray contains 8group
$check=false;
for($i=0; $i<count($GroupOfEight);$i++){
//$containsSearch = count(array_intersect($search_this,$all)) == count($search_this);
$containsSearch = (count(array_intersect($stackArray,$GroupOfEight[$i])) == count($stackArray) && count(array_intersect($stackArray,$GroupOfEight[$i])) == count($GroupOfEight[$i]));
if($containsSearch && !$check){
echo $i; //This specifies which index in GroupOfEight contains a matching array
$check=true;
}
}
?>

EDIT: Made a function. Returns first matched index or -1 for no matches:

function searcheight($stackArray,$GroupOfEight){
for($i=0; $i<count($GroupOfEight);$i++){
$containsSearch = (count(array_intersect($stackArray,$GroupOfEight[$i])) == count($stackArray) && count(array_intersect($stackArray,$GroupOfEight[$i])) == count($GroupOfEight[$i]));
if($containsSearch){
return $i; //This specifies which index in GroupOfEight contains a matching array
}
}
return -1;
}
echo searcheight($stackArray,$GroupOfEight);

How to check if an array contains another array in PHP?

One possible approach:

function look_for_array(array $test_var) {
foreach ($test_var as $key => $el) {
if (is_array($el)) {
return $key;
}
}
return null;
}

It's rather trivial to convert this function into collecting all such keys:

function look_for_all_arrays(array $test_var) {
$keys = [];
foreach ($test_var as $key => $el) {
if (is_array($el)) {
$keys[] = $key;
}
}
return $keys;
}

Demo.

Detect if array contains another array

Try this:

@if(isset($nh['Kod']))
<option value="{{ $nh['Kod'] }}">{{ $nh['KOY_Adi'] }}</option>
@else
@foreach($nh as $nhArray)
<option value="{{$nhArray['Kod']}}">{{$koyArray['KOY_Adi']}}</option>
@endforeach
@endif

Some explanations for the answer: in the if statement, you check the array key ['Kod'], if the array key exists, then you know this all the places are not displayed in the array, and then in the else statement, you do what you did before, to loop through the array.



Related Topics



Leave a reply



Submit