0

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

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

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..
@Christiaan Please add your code to the question, otherwise I can't tell what might be wrong.
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));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.