I have a dynamic amount of arrays in a specific array.
Let's call this specific array: FatherArray
This FatherArray has a dynamic amount of arrays in it, right now for example: Child1Array,Child2Array. Next time it gets called it could have more or less than those 2 Child(number)Arrays.
So I want to use the function array_intersect() with the arrays (children) of FatherArray as parameters, so like array_intersect(Child1Array,Child2Array).
I don't have a clue how i could do this dynamically, neither could I find anything about it, any help would greatly be appreciated
Add a comment
|
1 Answer
If your version is reasonably new (v5.6):
array_intersect(...$FatherArray);
Otherwise:
call_user_func_array('array_intersect', $FatherArray);
Demo: see comment by Mark (thx @MarkBaker)
4 Comments
Mark Baker
Christiaan
Thanks, however it returns an empty array for me. When i manually add it like array_intersect($FatherArray[0],$FatherArray[1]) it works fine. Thanks for the help so far tho! Edit: nvm, i am dumb. FatherArray[2] was empty..
Yoshi
@Christiaan Please add your code to the question, otherwise I can't tell what might be wrong.
Mark Baker
If you want to "ignore" empty child arrays, then
$result = array_intersect(...array_filter($fatherArray)); or call_user_func_array('array_intersect', array_filter($fatherArray));