PHP取不定个数数组交集

最近有个需求,有一个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 '
';
    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 '
';
    print_r($result_array);// ["16","2","3"]

你可能感兴趣的:(PHP取不定个数数组交集)