Jump to content

[SOLVED] Counting 2 arrays


nezbo

Recommended Posts

Hi all

 

I have 2 Arrays one is derived form a mysql and the other is a Split array.

 

What I am trying to do is create a report that counts up the number of items in the Split array that match the value in the mysql query.

 

 

Mysql      Split

Array      Array

Site 1    53

Site 2    17

etc...    etc...

 

I am not to sure where to start...

 

I have done something like this before but it was very messy and I am sure the must be a better way?

 

Neil

Link to comment
https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/
Share on other sites

<?php
$matches = 0;
foreach ($split as $split){
       foreach($mysql as $mysql){
               if (strcmp($split, $mysql) == 0){ //check for a match
                       ++$matches; //count the match
                       break; //break out of mysql foreach loop and continue the parent loop.
               }
        }
}
?>

this is case sensitive. for case insensitivity, use strcasecmp().

Cheers for this, but i am a bit confused about how to get mysql and split info in to it and out of it...

This is what i have so far?

 

$GetAllSites = @mysql_query("SELECT dental_emerg.userID, dental_emerg.sitePref, dental_emerg.timeStamp, dental_emerg.valid 
									FROM dental_emerg
									INNER JOIN
									(
										SELECT userID, MAX(timeStamp) as latest 
										FROM dental_emerg
										GROUP BY userID
									) as X
									ON dental_emerg.userID = X.userID AND dental_emerg.timeStamp = X.latest WHERE dental_emerg.valid = 0");
		while ($GetAllSites2 = @mysql_fetch_array($GetAllSites))
		{
			$setVSites = @mysql_query("SELECT * FROM site WHERE Emerg = 1 and valid = 0");

			$matches = 0;
			foreach ($split as $split){
				   foreach($mysql as $mysql){
						   if (strcmp($split, $mysql) == 0){ //check for a match
								   ++$matches; //count the match
								   break; //break out of mysql foreach loop and continue the parent loop.
						   }
					}
			}
		}

 

<?php
$matches = 0;
foreach ($split as $split){
       foreach($mysql as $mysql){
               if (strcmp($split, $mysql) == 0){ //check for a match
                       ++$matches; //count the match
                       break; //break out of mysql foreach loop and continue the parent loop.
               }
        }
}
?>

 

Case Sensitivity dosnt matter cos i am using numeric ID and i want to link that to the Site name etc...

this is case sensitive. for case insensitivity, use strcasecmp().

first of all, if your using numeric comparison, use

 

if ($split == $mysql){

 

instead of

 

if (strcmp($split, $mysql) == 0){

 

i'm assuming $GetAllSites2 is the $mysql array, and $setVSites is the $split. so first you want to

$setVSites2 = mysql_fetch_array($setVSites);

 

then swap all $split for $setVSites2 and all $mysql for $GetAllSites2. then the code will search for each value in $setVSites2 in the array $GetAllSites2, and count the amount of matches into $matches.

 

to use the result, simply use $matches somewhere after the loop.

I think i am starting to understand... :)

 

I now have this code :

 

		$GetLastEntry = @mysql_query("SELECT dental_emerg.userID, dental_emerg.sitePref, dental_emerg.timeStamp, dental_emerg.valid 
									FROM dental_emerg
									INNER JOIN
									(
										SELECT userID, MAX(timeStamp) as latest 
										FROM dental_emerg
										GROUP BY userID
									) as X
									ON dental_emerg.userID = X.userID AND dental_emerg.timeStamp = X.latest WHERE dental_emerg.valid = 0");
		while ($GetLastEntry2 = @mysql_fetch_array($GetLastEntry))
		{
			$setVSites = @mysql_query("SELECT * FROM site WHERE Emerg = 1 and valid = 0"); //This is the mysql
			$split = split(";", $GetLastEntry2['sitePref']; //this is the split
			$matches = 0;
			foreach ($split as $split){
				   foreach($setVSites as $setVSites){
						   if ($split == $mysql){ //check for a match
								   ++$matches; //count the match
								   break; //break out of mysql foreach loop and continue the parent loop.
						   }
					}
			}
		}

 

So how do I get the information on the screen?

the code wont work, because mysql_query() returns an object, not an array, so $setVSites isn't an array and connot be used in a foreach() loop.

 

to print the number of matches to screen, just use

 

echo $matches;

 

to get $setVSites into an array you have to use

 

$someVariable = mysql_fetch_array($setVSites);

 

the problem is that as far as i know, $setVSites may contain less rows than $GetLastEntry2, so at some point you may be trying to access a row that doesn't exist (because the loop will continue untill there are no more rows in $GetLastEntry2).

Cheers for all the advice but i am not to sure how to get it to work so i have now come up with this code...

This also still dosnt work and i am not to sure why?

 

$GetLastEntry = @mysql_query("SELECT dental_emerg.userID, dental_emerg.sitePref, dental_emerg.timeStamp, dental_emerg.valid 
									FROM dental_emerg
									INNER JOIN
									(
										SELECT userID, MAX(timeStamp) as latest 
										FROM dental_emerg
										GROUP BY userID
									) as X
									ON dental_emerg.userID = X.userID AND dental_emerg.timeStamp = X.latest WHERE dental_emerg.valid = 0");

		$setVSites = @mysql_query("SELECT * FROM site WHERE Emerg = 1 and valid = 0"); //This is the mysql for getting the sites
		while ($someVariable = @mysql_fetch_array($setVSites))//convert the mysql object to array
		{
			$numberZero = 0;
			echo $someVariable['SiteName'] . "=";
			while ($GetLastEntry2 = @mysql_fetch_array($GetLastEntry))//convert the mysql object to array
			{
				$split = split(";", $GetLastEntry2['sitePref']); //this is the split
				$countSplit = count($split); //count the number of splits
				for($i=0; $i <= $countSplit; $i++)
				{
					if ($split[$i] == $someVariable['SiteID'])
					{
						$numberZero = $numberZero + 1;
					}
				}
			}
			echo $numberZero;
			echo "<br>";
		}

 

 

I am getting the results of all the sites :

 

But I am only getting the results for 1 site and 0 for the rest, i know there are some i there...

 

PCC=1

St Peters Centre=0

Darwen Health Centre=0

Bacup Health Centre=0

Haslingden Health Centre=0

Howard Street Community=0

Larkhill Health Centre=0

Montague Health Centre=0

Special Care Dentistry (RBH)=0

Colne Sure Start=0

Yarnspinners PCC=0

Clitheroe Hospital=0

RBH (GA Office)=0

Oak House Dental Centre=0

 

;D

 

The working code ::

$setVSites = @mysql_query("SELECT * FROM site WHERE Emerg = 1 and valid = 0"); //This is the mysql for getting the sites
		while ($someVariable = @mysql_fetch_array($setVSites))//convert the mysql object to array
		{
			echo "<tr><td>";
			$GetLastEntry = @mysql_query("SELECT dental_emerg.userID, dental_emerg.sitePref, dental_emerg.timeStamp, dental_emerg.valid 
									FROM dental_emerg
									INNER JOIN
									(
										SELECT userID, MAX(timeStamp) as latest 
										FROM dental_emerg
										GROUP BY userID
									) as X
									ON dental_emerg.userID = X.userID AND dental_emerg.timeStamp = X.latest WHERE dental_emerg.valid = 0");
			$numberZero = 0;
			echo $someVariable['SiteName'];
			while ($GetLastEntry2 = @mysql_fetch_array($GetLastEntry))//convert the mysql object to array
			{
				$splited = split(";", $GetLastEntry2['sitePref']); //this is the split
				$countSplit = count($splited); //count the number of splits
				for($i=0; $i <= $countSplit; $i++)
				{
					if ($splited[$i] == $someVariable['SiteID'])
					{
						$numberZero = $numberZero + 1;
					}
				}
			}
			echo "</td><td> = " . $numberZero . "</tb></tr>";


		}

 

 

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.