Match any specified items in an array

Discussion in 'PHP' started by Omzy, Dec 20, 2008.

  1. #1
    Lets say I have an array called $values

    I use a series of IF statements to add items to the array using array_push

    I now want to use an IF statement to check if ANY of the possible items exist in the array.

    Normally I'd do something like:

    if (in_array('item1', $values) || in_array('item2', $values) || in_array('item3', $values))

    But is there another (simpler) way of doing this? Can I make the list of all possible items and put that in an array or something?
     
    Omzy, Dec 20, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    function any_in_array( $items = array(), &$array, $strict = false )
    {
        foreach ( $items as $item )
            if ( in_array($item, $array, $strict) )
                return true;
        return false;
    }
    
    function all_in_array( $items = array(), &$array, $strict = false )
    {
        foreach ( $items as $item )
            if ( !in_array($item, $array, $strict) )
                return false;
        return true;
    }
    PHP:
    I would assume you want the any_in_array function.
     
    Danltn, Dec 20, 2008 IP
  3. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hmmmm I didn't expect to have to use a function! It would have been good if the "in_array" could take multiple search terms! :-|
     
    Omzy, Dec 20, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    The function is just a wrapper, take it out of the function if you like, but it's likely you'll have to do something similar somewhere else in your code, and I'm a supporter of the DRY principle.

    Dan.

    Example usage :
    
    $items = array('name', 'phone', 'something', 'else');
    $values = array('whatever');
    if(any_in_array($items, $values)) {
    /* Do something */
    } else {
    /* Do something else */
    }
    PHP:
     
    Danltn, Dec 20, 2008 IP
  5. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    There has GOT to be a simpler way of doing this!

    Something along the lines of:

    if (in_array('item1' || 'item2' || 'item3', $values))

    Obviously that doesn't work but it seems odd that something like this isn't part of the standard PHP functionset!
     
    Omzy, Dec 21, 2008 IP
  6. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I got the solution I was looking for!

    $newItems = array('item1', 'item2', 'item3');
    $testArray = array_intersect($values, $newItems);
    Code (markup):
    From another forum ;)
     
    Omzy, Dec 21, 2008 IP