Compare 2 arrays

Discussion in 'PHP' started by niel, Oct 16, 2008.

  1. #1
    Hi, I need a little help. I trie to make a list of cocktails and a search list for them. So I got a few recepies in the database, and now to the search form I can choose which ingrediency do I have at home lets say "vodka, gin, rum, grenadina, orange jouce" and now I need to get only those drinks I can mix whit this ingrediencies. I tried to do it with comparing 2 array but without luck. Can please somebody help and come up with an idea how could I make it?
     
    niel, Oct 16, 2008 IP
  2. Ueland

    Ueland Peon

    Messages:
    66
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you have it in a database, you can explode the array with your search terms and use a query a-la:

    "select foo from table where name in('drink1', 'thing2', thing3')".

    :)
     
    Ueland, Oct 16, 2008 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    you can try creating need a database of ingredients that is universal in all your cocktails. you then need to assign 'tags' to each recipe, much like in wordpress: eg. 'martini', 'tequila', 'Tabasco sauce'

    you can then create a tickbox or whatever tag cloud list that allows the users to pick what they have.

    then as you go through the cocktails, run a little function that returns true/false, in which you take each individual cocktail tag and check if its in_array against the tags of ingredients available. assume $ok = true; until you find an ingredient that is not there, for example:
    
    <?PHP
    
    // populate what they have from user input but controlled to pre-defined tags
    $inFridge = Array("Vodka", "Tonic", "Bitter Lemon", "Lime", "Bourbon", "Coke", "Tomato Juice", "Black Pepper", "Salt", "Sugar");
    // populate cocktail tags from db
    $cocktails = Array(
        "Bloody Marry" => Array("Vodka", "Tomato Juice", "Black Pepper", "Salt"),
        "Screwdriver" => Array("Vodka", "Orange Juice")
    );
    
    function canDo($what) {
        global $inFridge;
    
        $can = true;
        foreach($what as $item) {
            if (!in_array($item, $inFridge)) {
                $can = false;
                echo "Don't have $item<br />";
            }
        }
        return $can;
    }
    
    // check cocktails...
    foreach($cocktails as $name => $needed) {
        echo (canDo($needed)) ? "You can make $name<br />" : "You can't do $name<br />";
    }
    
    ?>
    
    PHP:
    outputs:
    You can make Bloody Marry
    Don't have Orange Juice
    You can't do Screwdriver
    Code (markup):
    given time i'd improve it but it's a start.
     
    dimitar christoff, Oct 16, 2008 IP
  4. niel

    niel Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    dimitar christoff's: thanks a lot lot lot, you wrote me more than I needed. One more time thanks a lot
     
    niel, Oct 16, 2008 IP
  5. niel

    niel Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    somehow I cant get the drinks from the database into this format
    $cocktails = Array(
    "Bloody Marry" => Array("Vodka", "Tomato Juice", "Black Pepper", "Salt"),
    "Screwdriver" => Array("Vodka", "Orange Juice")
    );

    I have one table with the name of the cocktail and another with the ingrediency which are stored like this:
    $ingrediency = Array("Vodka", "Tomato Juice", "Black Pepper", "Salt");

    I tried different ways to make some loop function to get all drinks from the DB into this format:
    $cocktails = Array(
    "Bloody Marry" => Array("Vodka", "Tomato Juice", "Black Pepper", "Salt"),
    "Screwdriver" => Array("Vodka", "Orange Juice")
    );

    but it was never working :(
     
    niel, Nov 10, 2008 IP
  6. intelrate

    intelrate Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Could you please provide columns of these two tables from your DB? There should be nothing complicated but data schema is important.

    And question to you. Is there any difference in order of ingredients? As I know from practice - it is! :)
     
    intelrate, Nov 10, 2008 IP
  7. niel

    niel Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    it is in one table:
    1 column: id of the drink (formating int)
    2 column: name of the drink (formating varchar)
    3 column: ingrediences (formating varchar - stored in array $ingrediency = Array("Vodka", "Tomato Juice", "Black Pepper", "Salt");)
    other columns for how to prepare the drink, serving glass, mixing method...


    and yes there is a difference in order of the ingrediences but we dont use it.
     
    niel, Nov 10, 2008 IP
  8. intelrate

    intelrate Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thus, meta code is:

    
    $cocktails = array();
    
    while ($row = your_persistence_engine_fetch_array(...)) {
        $cocktails[$row['cocktail']] = array();
        $ingredient = strtok($row['ingredient'],"your splitter");
        while ($ingredient)) {
            $cocktails[$row['cocktail']][] = $ingredient;
            $ingredient = strtok("your splitter");
        }
    }
    
    PHP:
    Nevertheless, it would be better to normalize your DB schema because you'll get crazy when you application grows up to 10 000 lines.
     
    intelrate, Nov 10, 2008 IP