Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Currently I have 2 array:

array(1, 2, 3, 4);
array(4, 5, 6, 7);

How can I check if there is at least one equal value in both of them? (The example above has 1 equal value => 4, so the function should return true).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
554 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...