使用 PHP 在多个数组中查找公共值 [英] Find common values in multiple arrays with PHP

查看:23
本文介绍了使用 PHP 在多个数组中查找公共值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在多个数组中找到共同的值.数组的数量可能是无限的.示例(来自 print_r 的输出)

I need to find common values in multiple arrays. Number of arrays may be infinite. Example (output from print_r)

Array1
(
    [0] => 118
    [1] => 802
    [2] => 800
)
Array2
(
    [0] => 765
    [1] => 801
)
Array3
(
    [0] => 765 
    [1] => 794
    [2] => 793
    [3] => 792
    [4] => 791
    [5] => 799
    [6] => 801
    [7] => 802
    [8] => 800
)

现在,我需要找到所有 3 个(或更多,如果可用)的通用值......我该怎么做?

now, I need to find the values that are common on all 3 (or more if available) of them.... how do I do that?

谢谢

推荐答案

array_intersect()

$intersect = array_intersect($array1,$array2,$array3);

如果你不知道你有多少个数组,那么建立一个数组和用户的数组 call_user_func_array()

If you don't know how many arrays you have, then build up an array of arrays and user call_user_func_array()

$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);

这篇关于使用 PHP 在多个数组中查找公共值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆