Forum Moderators: coopster
Array
(
[0] => Array
(
[term_match] => example1.com
[SourceROOT] => website1.com
)
[1] => Array
(
[term_match] => example1.com
[SourceROOT] => website2.com
)
[2] => Array
(
[term_match] => example1.com
[SourceROOT] => website3.com
)
[3] => Array
(
[term_match] => example2.net
[SourceROOT] => website1.com
)
[4] => Array
(
[term_match] => example2.net
[SourceROOT] => website9.com
)
How would I loop through this data so that if I enter a query against the array with "example.com" and "example2.net", it returns website1.com (the term_match they have in common).
Thanks in advance!
$FirstWebsite="example1.com";
$SecondWebsite="example2.net";
for($i=0; $i < count($array); $i++) {
if($FirstWebsite==$array[$i][term_match]) {
$SourceROOT[]==$array[$i][SourceROOT];
}
}
for($i=0; $i < count($array); $i++) {
if($SecondWebsite==$array[$i][term_match]) {
for($ii=0; $ii < count($SourceROOT); $ii++) {
if($SourceROOT[$ii]==$array[$i][SourceROOT]) {
$MatchFound=SourceROOT[$ii];
break;
} // end if
} // end inner for
if(isset($MatchFound)) {
break;
} // end if
} // end 1st if
} // end 1st for
//go through the targetID array
foreach($targetArr as $tempArr){
//as long as your array structure never varies this
//will work otherwise it won't be cautious!
if(trim($tempArr['term_match']) == "example1.com"){
echo("We matched example1.com to {$tempArr["SourceRoot"]}<br>");
}//if
elseif($tempArr['term_match'] == "example2.com"){
echo("We matched example2.com to {$tempArr["SourceRoot"]}<br>");
}//elseif
else{
echo("I couldn't find a match for {$tempArr["SourceRoot"]}<br>");
}//else
}//foreache $targetArr
The above makes the first set of matches, but does not deliver the match between the matches and puts all the results into $tempArr, but there needs to be one more level of matching before the 'result' being determined, so it really doesn't work without being re-worked (or added to) a bit.
In looking at it, mine could possibly be made a bit more efficient but should work and be fairly efficient as is (or close to 'as is' WRT 'copy / paste' usability).
In any case, it is now working properly, thanks! My next problem is that I cant seem to wrap my head around how I can adapt this to be dynamic.
Basically, this needs to be able to compare anywhere from 2 to 20 URL's, depending on what is submitted through a form on a different page. Thoughts?
If you have 20 websites and there will always be 20 $SourceROOT matches you really only need the current working code, because you know if you match two you match all 20, but if it's possible when there are 20 websites 5 will have the same $SourceROOT and 15 will have a different one, or 3, 5, 12 or some other random combination and you're looking for the highest number of matches it gets a bit more complicated.
IOW: With 20 $SourceROOT matches required if one doesn't match you know the highest possible number of matches you will have is 19, so it's not the $SourceROOT, but as soon as one matches, you know what the 'common $SourceROOT' is, because all submitted sites have to match the same $SourceROOT and the only way to get to 20 out of 20 sites matching is for them to all match, which means if two match the same $SourceROOT you found what you are looking for.
/* Store all websites submitted as an array. $Websites[]; I'll let you decide how. */
for($i=0; $i < count($Websites); $i++) {
for($ii=0; $ii < count($array); $ii++) {
if($Websites[$i]==$array[$ii]['term_match']) {
$PosssibleSourceROOT[]=$array[$ii]['SourceROOT'];
} // End if
} // End inner for
} // End outer for
$FindMostMatches=array_count_values($PosssibleSourceROOT);
if(count($FindMostMatches)>1) {
sort($FindMostMatches);
}
$BestMatch=$FindMostMatches[0];
echo key($BestMatch);
/* Not sure what to do in case of a tie, but this should work or get you close and give you an idea of what to do. */
/* If you only need to match 2 since all 20 have to match, you could still store your POSTed websites as an array $Websites[]; then use */
for($i=0; $i < 2; $i++) {
for($ii=0; $ii < count($array); $ii++) {
if($Websites[$i]==$array[$ii]['term_match']) {
$PosssibleSourceROOT[]=$array[$ii]['SourceROOT'];
} // End if
} // End inner for
} // End outer for
$FindMostMatches=array_count_values($PosssibleSourceROOT);
if(count($FindMostMatches)>1) {
sort($FindMostMatches);
}
$BestMatch=$FindMostMatches[0];
echo key($BestMatch);
Yes, it takes quite a bit of time, but when I was done, I knew basically what there was a function already built in to do and even if I didn't (don't) always remember the exact function or syntax so I usually know what's possible with built in functions, and it's easy to reference with a bookmark or quick search to find what I want to do and build it into a loop...