【问题标题】:How would i use array_intersect() with the arrays in an array?我如何将 array_intersect() 与数组中的数组一起使用?
【发布时间】:2018-04-22 10:18:43
【问题描述】:

我在特定数组中有动态数量的数组。
我们称这个特定数组为:FatherArray

这个 FatherArray 中包含动态数量的数组,例如:Child1ArrayChild2Array。下次它被调用时,它可能比这 2 个 Child(number)Arrays 多或少。

所以我想用FatherArray的数组(子)作为参数的函数array_intersect(),比如array_intersect(Child1Array,Child2Array)。
我不知道如何动态地做到这一点,我也找不到任何关于它的信息,任何帮助将不胜感激

【问题讨论】:

    标签: php arrays parameters parameter-passing optional-parameters


    【解决方案1】:

    如果您的版本相当新 (v5.6):

    array_intersect(...$FatherArray);
    

    否则:

    call_user_func_array('array_intersect', $FatherArray);
    

    演示:查看 Mark 的评论(感谢 @MarkBaker)

    【讨论】:

    • 谢谢,但是它为我返回了一个空数组。当我像 array_intersect($FatherArray[0],$FatherArray[1]) 一样手动添加它时,它工作正常。感谢您迄今为止的帮助!编辑:nvm,我很笨。 FatherArray[2] 为空..
    • @Christiaan 请将您的代码添加到问题中,否则我无法判断可能出了什么问题。
    • 如果你想“忽略”空子数组,那么$result = array_intersect(...array_filter($fatherArray));call_user_func_array('array_intersect', array_filter($fatherArray));
    猜你喜欢
    • 2021-03-01
    • 2017-04-23
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2013-12-19
    • 2012-01-20
    • 2020-12-04
    • 1970-01-01
    相关资源
    最近更新 更多