find one of many variables within $array

Marcus

Well-known member
Currently I do this, to check if there are values (here: "option x") within an array (here $user...):

if(count(array_diff(array("option 1", "option 2","option 3"), $user['customFields']['cake']))<3)

I use count()<count($check_array) to check once a value was found. Do you have a better and more straight forward solution for this?
 
Currently I do this, to check if there are values (here: "option x") within an array (here $user...):

if(count(array_diff(array("option 1", "option 2","option 3"), $user['customFields']['cake']))<3)

I use count()<count($check_array) to check once a value was found. Do you have a better and more straight forward solution for this?

http://php.net/manual/en/function.in-array.php ?

Code:
in_array($user['customFields']['cake'], $options);
 
Hi Rigel,

this is right, but in my case $user['customFields']['cake'] is also an array.

The problem is, that I want to find multiple values within an array.

As an example, there is an array full of vegetables. And I have an basked of what I bought at the supermarket. I want to find out if I bought some vegetables. Currently I do it like this: count(array_diff($basket, $vegetables)<count of max difference of either $basket or $vegetables).

In my current easier scenario it is that the first array contains have no, one or multiple values of the second array.
 
Hi Rigel,

this is right, but in my case $user['customFields']['cake'] is also an array.

The problem is, that I want to find multiple values within an array.

As an example, there is an array full of vegetables. And I have an basked of what I bought at the supermarket. I want to find out if I bought some vegetables. Currently I do it like this: count(array_diff($basket, $vegetables)<count of max difference of either $basket or $vegetables).

In my current easier scenario it is that the first array contains have no, one or multiple values of the second array.
I see.
Check this answer: http://stackoverflow.com/questions/7542694/in-array-multiple-values
 
Top Bottom