tags:

views:

336

answers:

2

Hi,

I have two arrays in PHP as follows:

People:

Array
(
    [0] => 3
    [1] => 20
)

Wanted Criminals:

Array
(
    [0] => 2
    [1] => 4
    [2] => 8
    [3] => 11
    [4] => 12
    [5] => 13
    [6] => 14
    [7] => 15
    [8] => 16
    [9] => 17
    [10] => 18
    [11] => 19
    [12] => 20
)

How do I check if any of the People elements are in the Wanted Criminals array?

In this example, it should return true because 20 is in Wanted Criminals.

Thanks in advance.

+5  A: 

You can use array_intersect().

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

Greg
Can't use empty() with anything other than a variable.
grantwparks
A: 

That code is invalid as you can only pass variables into language constructs. Empty() is a language construct.

You have to do this in two lines.

$result = array_intersect($people, $criminals);
$result = !empty($result);
Paul Dragoonis
The problem is not it is a language construct. The problem is it expects a reference and Greg's passing a value.
Artefacto