Welcome to WebmasterWorld Guest from 23.20.241.155

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Looping through array and finding matches

     
6:32 pm on Oct 5, 2009 (gmt 0)

5+ Year Member



I have a MySQL query that is returning the following data in an array:

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!

8:12 pm on Oct 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm fairly tired, but this should get you close, or at least give you some ideas. (I'm assuming you meant example1.com and example2.net since example.com is not in your array, and also meant the SourceROOT they have in common not the term_root... If not you should be able to adjust it easily.)

$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

8:32 pm on Oct 5, 2009 (gmt 0)

5+ Year Member



Yes you are correct in your assumptions, thanks! I will give this a shot and let you know how it goes.
8:47 pm on Oct 5, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



As long as your Array structure stays the same.
$targetArr = Array("5" => Array("term_match" => "example1.com", "SourceRoot" => "http://www.example1.net"),
"6" => Array("term_match" => "example2.com", "SourceRoot" => "http://example2.net"),
"7" => Array("term_match" => "example1.com" , "SourceRoot" => "http://second.example1.net"),
"8" => Array("term_match" => "example2.com", "SourceRoot" => "http://second.example2.net"));

//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

2:42 am on Oct 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, the above only works for the first match... The issue is the code needs to make a secondary comparison based on the first set of matches to determine the matching SourceROOT between the initial set of matches.

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).

5:23 am on Oct 6, 2009 (gmt 0)

5+ Year Member



jd01 I have not been able to get your code to run. I have isolated the problem to this line:

$MatchFound=SourceROOT[$ii];

When that line is present, the page never loads.

5:50 am on Oct 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot the $ ... :)

$MatchFound=$SourceROOT[$ii];

2:35 pm on Oct 7, 2009 (gmt 0)

5+ Year Member



Wow I cant believe I didnt notice that myself! Too many hours in front of the computer I suppose...

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?

3:41 pm on Oct 7, 2009 (gmt 0)

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



It's really a very specific question you're asking, because:

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.

4:41 pm on Oct 7, 2009 (gmt 0)

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Here, this should be completely flexible:

/* 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);

6:55 pm on Oct 7, 2009 (gmt 0)

5+ Year Member



Thanks for the response! Ill give this a shot and let you know how it goes!
7:06 pm on Oct 7, 2009 (gmt 0)

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You might need asort() rather than sort(). I don't always remember off the top of my head and didn't test, so if you don't get the right key() use asort() and you should... Too many languages bouncing around sometimes. :)
8:04 pm on Oct 7, 2009 (gmt 0)

5+ Year Member



$BestMatch=$FindMostMatches[0];
echo key($BestMatch);

This does not seem to return anything. I did a print_r on $BestMatch and it shows the array, but im thinking because there is no 0 key in the array thats why the echo statement isnt working?

8:09 pm on Oct 7, 2009 (gmt 0)

5+ Year Member



nevermind, I got it working how I needed it. Thanks again for your help!
10:47 pm on Oct 7, 2009 (gmt 0)

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Glad you got it working... I work with quite a few different languages and one things I found really helpful when I started with PHP was taking a couple hours and reading through the array functions [php.net...] and the string functions [php.net...]

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...

 

Featured Threads

Hot Threads This Week

Hot Threads This Month