dsjbest2015
dsjbest2015
2015-11-26 04:06

检查两个数组中的任何匹配[重复]

已采纳

This question already has an answer here:

I have array like this

a1 = {1,3,5,7,9};
a2 = {2,4,6,8,9};

In both the arrays, there is a common value 9. I would like to know if there is a built in php function present which returns true or false if at least one value matches or not.

Thanks

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongmei1828 dongmei1828 6年前

    You could use array_intersect

    $a1 = [1,3,5,7,9];
    $a2 = [2,4,6,8,9];
    $a3 = [10];
    
    array_intersect($a1,$a2) ? true : false;
    // => true
    
    array_intersect($a1,$a3) ? true : false;
    // => false
    
    点赞 4 评论 复制链接分享
  • doujiyan0031 doujiyan0031 6年前

    You can use array_intersect function in PHP. Documentation.

    Example

    $a = array(1, 3, 5, 7);
    $b = array(1, 2, 3, 4);
    
    
    
    $haveMatch =  (array_intersect($a, $b))?true:false;
    echo $haveMatch;
    
    点赞 4 评论 复制链接分享

相关推荐