jenskim Posted April 26, 2010 Share Posted April 26, 2010 Hello purples. I'm working on a project that wants to compare two arrays and display all matches. I'm almost there... comparing two arrays seems to be working, but only one piece of the match is displaying. List1 is a database field looking at ingredients that are entered by the user. List2 is a list of toxic ingredients. When a user enters a word that matches the list of toxics (list2) that word is displayed on the Web page. However only ONE of the matched words displays - when there are more than one matches. Anybody run into this? $list1 = $row['ingredients']; $list2 = array("2-butoxyethanol","acetone","ammonia","aerosol","bleach","butylcellosolve","d-limonene","DEA","diethyleneglyco","diethanolamine","ethoxylatednonylphenol","formaldehyde","fragrance","methylenechloride","monoethanolamine","morpholine","NPE","NTA","naphthalene","nonylphenolethoxylate","paraben","paradichlorobenzene","perchloroethylene","petroleum","phosphate","phosphoricacid","silica","sodiumdichloroisocyanuratedihydrate","sodiumhypochlorite","sodiumlaurylsulfatetoluene","trisodiumnitrilotriacetate","turpentine","xylene"); $tmp1 = explode(',',$list1);//ingredients $tmp2 = explode(',',$list2);//toxics for($i = 0; $i <count($tmp1); $i++){ $pieces = $tmp1[$i]; if (in_array($pieces, $list2)) { echo "<span style=\"color:red;\">Toxics exist: " . $pieces . "</span>"; } } Quote Link to comment Share on other sites More sharing options...
de.monkeyz Posted April 26, 2010 Share Posted April 26, 2010 It worked for me. Maybe your values have extra white space? Try $pieces = trim($tmp1[$i]); Quote Link to comment Share on other sites More sharing options...
jenskim Posted April 26, 2010 Author Share Posted April 26, 2010 Thanks Monkeyz... ya, it WORKS for me, but it's not returning ALL the matches. For instance, in the toxics list there is acetone and bleach. If I enter acetone, bleach as ingredients for a product, only acetone is displayed on the page. So, only the FIRST match shows.... Whah... very frustrating not understanding these things. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 26, 2010 Share Posted April 26, 2010 Why are you exploding() $list2 when it is already an array? You don't even use $tmp2 anyway. Why are you using a for() loop to look for matches? You should use array_intersect() What EXACTLY does $row['ingredients'] look like. Are there spaces after the commas? if so, you will need to trim the values after exploding. <?php //This line for testing only $row['ingredients'] = "Acetone,formaldehyde,paraben"; $toxics = array( "2-butoxyethanol","acetone","ammonia","aerosol","bleach","butylcellosolve","d-limonene", "DEA","diethyleneglyco","diethanolamine","ethoxylatednonylphenol","formaldehyde","fragrance", "methylenechloride","monoethanolamine","morpholine","NPE","NTA","naphthalene","nonylphenolethoxylate", "paraben","paradichlorobenzene","perchloroethylene","petroleum","phosphate","phosphoricacid","silica", "sodiumdichloroisocyanuratedihydrate","sodiumhypochlorite","sodiumlaurylsulfatetoluene", "trisodiumnitrilotriacetate","turpentine","xylene"); $ingredients = explode(',', $row['ingredients']); //ingredients array_map("trim", $ingredients); //Trim the values //Find matches of $ingredients in $toxics $toxic_matches = array_intersect($ingredients, $toxics); //Output the matches foreach($toxic_matches as $match) { echo "<span style=\"color:red;\">Toxics exist: {$match}</span><br />"; } ?> NOTE: this does not handle cases where there is a different in the "case" of the letters. In the above "Acetone" will not be found as a match. If you need that functionality please state so. Quote Link to comment Share on other sites More sharing options...
jenskim Posted April 26, 2010 Author Share Posted April 26, 2010 aha. I'm seeing the light - I think that there may be spaces in the "ingredients" list. Thanks for that tip! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.