How can I use in_array if the needle is an array?
Asked Answered
P

5

66

I have 2 arrays, the value will be loaded from database, below is an example:

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);

What I want to do is to check if all the values in $arr1 exist in $arr2. The above example should be a TRUE while:

$arr3 = array(1,2,4,5,6,7);

comparing $arr1 with $arr3 will return a FALSE.

Normally I use in_array because I only need to check single value into an array. But in this case, in_array cannot be used. I'd like to see if there is a simple way to do the checking with a minimum looping.

UPDATE for clarification.

First array will be a set that contains unique values. Second array can contain duplicated values. They are both guaranteed an array before processing.

Pinochle answered 21/2, 2010 at 1:25 Comment(1)
Might be worth clarifying: Are you looking for normal "set" semantics? What result would you expect if the second array was array(1,2,4,3)? What if the first was array(1,1,1,1)? Or are they illegal?Bushhammer
M
103

Use array_diff():

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
$arr3 = array_diff($arr1, $arr2);
if (count($arr3) == 0) {
  // all of $arr1 is in $arr2
}
Moderate answered 21/2, 2010 at 1:31 Comment(3)
This is not working well when array values are string.Antidisestablishmentarianism
@Antidisestablishmentarianism did you find any alternate solution for string valuesHydrogeology
Why would anyone claim that array_diff() is unsuitable on arrays of strings? array_diff() is perfectly capable.Sparrow
D
46

You can use array_intersect or array_diff:

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);

if ( $arr1 == array_intersect($arr1, $arr2) ) {
    // All elements of arr1 are in arr2
}

However, if you don't need to use the result of the intersection (which seems to be your case), it is more space and time efficient to use array_diff:

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
$diff = array_diff($arr1, $arr2);

if ( empty($diff) ) {
    // All elements of arr1 are in arr2
}
Deplume answered 21/2, 2010 at 2:24 Comment(1)
The behavior of array_intersect is different that array_diff. array_intersect return common elements. If the $arr1 contains values that does not exist in $arr2 the result will be different of array_diff and this result will be wrong for the anwser.Shcherbakov
T
5

You can try use the array_diff() function to find the difference between the two arrays, this might help you. I think to clarify you mean, all the values in the first array must be in the second array, but not the other way around.

Tadio answered 21/2, 2010 at 1:28 Comment(1)
Yes Sam. All $arr1 values must be appear in second array, to get a TRUE condition, otherwise, FALSE. I'll take a look for the array_diff(). ThanksPinochle
L
0

In my particular case I needed to check if a pair of ids was processed before or not. So simple array_diff() did not work for me.

Instead I generated keys from ids sorted alphabetically and used them with in_array:

<?php
$pairs = array();
// ...
$pair = array($currentId, $id);
sort($pair);
$pair = implode('-', $pair);
if (in_array($pair, $pairs)) {
    continue;
}
$pairs[$pair] = $pair;

This is probably not an optimum solution at all but I just needed it for a dirty script to be executed once.

Lullaby answered 12/12, 2011 at 13:21 Comment(0)
P
0
function in_2array($needle,$haystack,$full_search=false){
    $ret = false;
    foreach($needle as $search){
        if(in_array($search,$haystack)){
            $ret = true;
            if(!$full_search){
                break;
            }
        }else if($full_search){
            $ret = false;
            break;
        }
    }
    return $ret;
}

if(in_2array($arr1,$arr2,true)){
echo 'It`s Ok!';
}
Pogge answered 11/7, 2023 at 4:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.