Riiibu
Riiibu

Reputation: 85

Getting notice "Array to string conversion" when using call_user_func_array()

I wanted to check for matching values in multiple arrays, so I made a multi-dimensional array by pushing them into $array and then wrote this line of code:

$result = call_user_func_array('array_intersect', $array);

I am getting the result I want, but I am always getting this notice on that particular line of code:

Notice: Array to string conversion

Wondering what's causing this. Hope someone can enlighten me.

Upvotes: 1

Views: 482

Answers (1)

Jon
Jon

Reputation: 437336

Your arrays (the first-level items inside $array) themselves contain arrays. This is unsupported by array_intersect, because it treats the array items as strings for purposes of determining equality:

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

I can't say definitely without knowing what exactly you are trying to do, but a possible solution is to use array_uintersect instead which will allow you to specify in code how to compare items without necessarily casting them to string.

Upvotes: 1

Related Questions