1. 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

Discussion in 'XenForo Development Discussions' started by Marcus, Nov 15, 2011.

  1. Marcus

    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?
  2. Rigel Kentaurus

    Rigel Kentaurus Well-Known Member

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

    in_array($user['customFields']['cake'], $options);
  3. Marcus

    Marcus Well-Known Member

    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.
  4. digitalpoint

    digitalpoint Well-Known Member

    The array_diff and array_intersect functions are for just that... finding stuff that matches (or missing) between multiple arrays. So I think you are doing it the best way possible.
    Marcus and Rigel Kentaurus like this.
  5. Rigel Kentaurus

    Rigel Kentaurus Well-Known Member

    I see.
    Check this answer: http://stackoverflow.com/questions/7542694/in-array-multiple-values
    Marcus likes this.

Share This Page