美文网首页
PHP取不定个数数组交集

PHP取不定个数数组交集

作者: willeny | 来源:发表于2018-07-27 15:22 被阅读0次

    最近有个需求,有一个N个二维数组,N是动态的,不固定个数,现需取这N个数组的交集内容。

    • 用到的函数是array_intersect_assoc
    • 用法
        $result_arr = array_intersect_assoc($arr_sourse[0],$arr_sourse[1],.......$arr_sourse[N-1]);
    
    • 数组内容
        $arr1 = ["16","2","3","6","9","17"];
        $arr2 = ["16","2","3","9","15"];
        $arr3 = ["16","2","3","6","8","17"];
    

    固定数组的用法

        $diff = array_intersect($arr1,$arr2,$arr3);
        echo '<pre>';
        print_r($diff);// ["16","2","3"]
    

    不固定数组用法

    • 只需用 call_user_func_array 来执行 array_intersect_assoc 就可以了
    • 具体用法
        $result_array = call_user_func_array ('array_intersect_assoc', array($arr1, $arr2,$arr3));
        echo '<pre>';
        print_r($result_array);// ["16","2","3"]
    

    相关文章

      网友评论

          本文标题:PHP取不定个数数组交集

          本文链接:https://www.haomeiwen.com/subject/xwfgmftx.html