Jump to content

Compare two arrays, display ALL matches


jenskim

Recommended Posts

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>";
		}
	}

Link to comment
https://forums.phpfreaks.com/topic/199789-compare-two-arrays-display-all-matches/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.