• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

find one of many variables within $array

Marcus

Well-known member
#1
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?
 

Rigel Kentaurus

Well-known member
#2
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);
 

Marcus

Well-known member
#3
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.
 

Rigel Kentaurus

Well-known member
#5
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