homepage Welcome to WebmasterWorld Guest from 54.167.75.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Looping through array and finding matches
martymac




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

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!

 

jd01




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

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

martymac




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

Yes you are correct in your assumptions, thanks! I will give this a shot and let you know how it goes.

andrewsmd




msg:4001723
 8:47 pm on Oct 5, 2009 (gmt 0)

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

jd01




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

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

martymac




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

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.

jd01




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

I forgot the $ ... :)

$MatchFound=$SourceROOT[$ii];

martymac




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

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?

TheMadScientist




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

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.

TheMadScientist




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

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

martymac




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

Thanks for the response! Ill give this a shot and let you know how it goes!

TheMadScientist




msg:4003150
 7:06 pm on Oct 7, 2009 (gmt 0)

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

martymac




msg:4003188
 8:04 pm on Oct 7, 2009 (gmt 0)

$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?

martymac




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

nevermind, I got it working how I needed it. Thanks again for your help!

TheMadScientist




msg:4003298
 10:47 pm on Oct 7, 2009 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved