array_intersect,但单个阵列变量的一个子阵列 [英] array_intersect, but for a sub-arrays of a single array variable

查看:95
本文介绍了array_intersect,但单个阵列变量的一个子阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,看起来像这样:

  $富=阵列(
    0 =>阵列('一个','B','C','D'),
    1 =>阵列('B','C','D'),
    2 =>阵列('B','D','F')

我将把 $ foo的[0] $ foo的[1] $ foo的[2] 作为子阵列。

基本上,我需要在所有3个子阵列中的 $ foo的执行 array_intersect()。结果应该是:

 阵列(B,D)

由于三个子阵列的共同之处这些值。什么是做到这一点的最好方法是什么?

一些注意事项:


  • 总会有至少一个子阵列。没有上限。

  • 如果只设置一个子阵列,它应该返回子阵列

  • 如果有不是所有的子阵列的任何共同的价值观,一个空数组应该返回

  • 如果这个功能已经作为一个PHP函数,我会/捂脸


解决方案

  $ =相交call_user_func_array('array_intersect',$ foo的);

注意,关键是从$ foo的preserved [0]

I've got an array that looks like this:

$foo = array(
    0 => array('a', 'b', 'c', 'd'),
    1 => array('b', 'c', 'd'),
    2 => array('b', 'd', 'f')
)

I'll refer to $foo[0], $foo[1], and $foo[2] as sub-arrays.

I basically need to perform an array_intersect() across all 3 sub-arrays in $foo. The result should be:

array('b', 'd')

As all three sub-arrays had these values in common. What is the best way to do this?

Some considerations:

  • There will always be at least one sub-array. No upper limit.
  • If only one sub-array is provided, it should return that sub-array
  • If there aren't any common values in all the sub-arrays, an empty array should be returned
  • If this functionality already exists as a PHP function, I will /facepalm

解决方案

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

Note that keys are preserved from $foo[0]

这篇关于array_intersect,但单个阵列变量的一个子阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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