多维数组中查找子阵列之间的共同价值 [英] Finding common value among sub-arrays in a multidimensional array

查看:130
本文介绍了多维数组中查找子阵列之间的共同价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

Array
(
    [0] => Array
        (
            [0] => 87
            [1] => 58
            [2] => 85
            [3] => 86
        )

    [1] => Array
        (
            [0] => 58
            [1] => 84
        )

    [2] => Array
        (
            [0] => 58
        )

)

上面这个数组就是一个例子。实际的数组是可变大小的,但结构是这样的。基本上,我想每一个第二级阵列上运行 array_intersect 并抓住值(数字),它是它们之间的共同。在这种情况下,这将是 58

This array above is an example. The actual array is of variable size, but structured like this. Basically, I'd like to run array_intersect on each second level array and grab the value (number) that is common between them. In this case, it would be 58.

我不太知道从哪里开始对这个。有什么建议?

I'm not quite sure where to start on this. Any advice?

推荐答案

这对我的作品:

function multi_intersect($arr) {
   $return = array();
   foreach ($arr as $a) {
       foreach ($arr as $b) {
           if ($a === $b) continue;
           $return = array_merge($return, array_intersect($a, $b));
       }
   }
   return array_unique($return);
}

应该让你:

Array
(
    [0] => 58
)

如果在至少两个子阵列中有一个共同的编号上面将工作

The above will work if you have a common number in at least two of the sub-arrays.

您的编辑之后的:

您可以简单地使用 call_user_func_array array_intersect ,如果你想找到包含在所有子号数组:

You can simply use call_user_func_array on array_intersect, if you want to find numbers that are contained in all sub-arrays:

$intersect = call_user_func_array('array_intersect', $arr);

这篇关于多维数组中查找子阵列之间的共同价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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