Obmerk Kronen
Obmerk Kronen

Reputation: 15949

in_array() works fine but throws error?

AFAIK - in_array() should return TRUE or FALSE.

In my case, It does validate as true - but still throwing an error:

[function.in-array]: Wrong datatype for second argument

The line is this :

in_array($key,$instance['cfl2']);

and the $instance['cfl2'] is a verified array which looks like this :

array(2) { [0]=> string(8) "price" [1]=> string(6) "age" }

My questions are :

I also tried $is = $instance['cfl2'] and in_array($key,$is) - but the result was the same error.

Upvotes: 0

Views: 715

Answers (2)

Bob  Sponge
Bob Sponge

Reputation: 4738

You can cast a variable to an array to avoid this error:

in_array($key, (array) $instance['cfl2'])

Upvotes: 2

Vishnu R
Vishnu R

Reputation: 1869

in_array() will deal as in_array("search", $instance).

If you are using a nested or multidiamentional array, then in_array() wont work and you should write a separate function to handle this. Or use array_key_exists() instead. It will work for certain specific situations. Find out if your requirement is met.

ie

if(array_key_exists($key,$instance['cfl2']))

Upvotes: 0

Related Questions