duanchouyi6730 2016-12-18 04:57
浏览 76
已采纳

如果两个数组中的任何元素匹配,则返回true? [重复]

This question already has an answer here:

I've two arrays, one is my define array and another I process from the database. Is there any proper way to compare between those arrays?

From database:

$user_type = [0 => "public" 1 => "10x413" 2 => "12x432"]

Defined array:

$specificUser = ['10x410','10x411','10x412','10x413','10x414'] 

If any element matches, then return true, just like php in_array() function.

</div>
  • 写回答

2条回答 默认 最新

  • doulu8415 2016-12-18 05:09
    关注

    Just loop and check as you would using in_array() to get the value that matches:

    foreach($specificUser as $value) {
        if(in_array($value, $user_type)) {
            echo $value;
            //break; to stop checking, a match was found, or not to continue and see more
        }
    }
    

    Or to just test for any match:

    if(array_intersect($specificUser, $user_type)) {
        // it's true :-)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥50 用AT89C52单片机设计一个温度测量与控制电路
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题