Jump to content

Recommended Posts

I have the following array and I need to get the count for how many times each appears in the array.  So basically during the loop it will add to the $ccc array the machine id, devid, inputdevid, eventtype and eventid - I'm looking for how many are exact matches and then output that sum and keep the information (machine, devid, inputdevid etc..). 

 

<?php
while ($row = mssql_fetch_assoc($qq)) {
$ccc[] = array('machine' => $row['MACHINE'], 'devid' => $row['DEVID'], 'inputdevid' => $row['INPUTDEVID'], 'eventtype' => $row['EVENTTYPE'], 'eventid' => $row['EVENTID']);

}

?>

 

 

I previously did it by using something along the lines of:

<?php
while ($row = mssql_fetch_assoc($q2)) {
  $ccc[$row['MACHINE']][$row['DEVID']][$row['INPUTDEVID']][$row['EVENTTYPE']][$row['EVENTID']][] = 1;
}
foreach ($ccc as $qq => $qqq) {
foreach ($qqq as $qqqq => $qqqqq) {
	foreach ($qqqqq as $qqqqqq => $qqqqqqq) {
		foreach ($qqqqqqq as $qqqqqqqq => $qqqqqqqqq) {
			foreach ($qqqqqqqqq as $qqqqqqqqqqq => $gg) {
				$i = 0;
				$i = count($zz["$qq"]["$qqqq"]["$qqqqqq"]["$qqqqqqqq"]["$qqqqqqqqqqq"]);
				$zzz["$qq"]["$qqqq"]["$qqqqqq"]["$qqqqqqqq"]["$qqqqqqqqqqq"] = $i;

			}
		}
	}
}
}

?>

 

But I don't like that way and it leads to some other issues (sorting the resulting array ($zzz) from highest to lowest so that I can get a top 10).

 

Any ideas or suggestions would be very much appreciated. Thanks

Link to comment
https://forums.phpfreaks.com/topic/166281-solved-counting-multi-dimensional-array/
Share on other sites

<?php
while ($row = mssql_fetch_assoc($qq)) {
$ccc[] = array(
	'machine'    => $row['MACHINE'],
	'devid'      => $row['DEVID'],
	'inputdevid' => $row['INPUTDEVID'],
	'eventtype'  => $row['EVENTTYPE'],
	'eventid'    => $row['EVENTID']
);
$machines[]   = $row['MACHINE'];
$devid[]      = $row['DEVID'];
$inputdevid[] = $row['INPUTDEVID'];
$eventtype[]  = $row['EVENTTYPE'];
$eventid[]    = $row['EVENTID'];
}

$machinesCount   = array_count_values($machines);
$devidCount      = array_count_values($devid);
$inputdevidCount = array_count_values($inputdevid);
$eventtypeCount  = array_count_values($eventtype);
$eventidCount    = array_count_values($eventid);

//The count arrays will be in this format
//array (
//  'value1' => 4, // <-- the number will be how many times
//  'value2' => 1, //     that value appears in the array
//  'value3' => 6,
//  'value4' => 3,
//)
?>

Actually, that's outputting the number of times that specific machine or devid et al. shows up in the array, but it's the combination that matters... So let's say:

Machine = 4

devid = 20

inputdevid = 11

eventtype = 4

eventid = 0

 

What I need to match is how many times all of those values are matched exactly together.  If the eventid changes to 1, that would be a separate match/count.

 

I'm thinking maybe I should just put them all under 1 key with a csv or some delimiter and then just explode in while iterating through a loop and match that way?

In case anyone is interested, here's how it ended up:

 

<?php
while ($row = mssql_fetch_assoc($qq)) {
$c[] = $row['MACHINE'] . "-" . $row['DEVID'] . "-" . $row['INPUTDEVID'] . "-" . $row['EVENTTYPE'] . "-" . $row['EVENTID'];
}

$d = array_count_values($c);
$g = array_unique($c);
foreach ($g as $e => $f) {
$zz["$f"] = $d["$f"];
}
arsort($zz);
foreach ($zz as $aa => $xx){
list($a1, $a2, $a3, $a4, $a5) = explode("-",$aa);

$sql6 = "SELECT READERDESC FROM READER WHERE PANELID = '" . $a1 . "'";
$sql6 .= " AND READERID = '" . $a2 . "'";
$tttt = mssql_query($sql6, $cxn2);
$row6 = mssql_fetch_assoc($tttt);

$sql7 = "SELECT NAME FROM ALARMINPUT WHERE PANELID = '" . $a1 . "'";
$sql7 .= " AND ALARMPID = '" . $a2 . "' AND INPUTID = '";
$sql7 .= $a3 . "'";
$ttttt = mssql_query($sql7, $cxn2);
$row7 = mssql_fetch_assoc($ttttt);

echo $xx . " | " . $row6['READERDESC'] . " | " . $row7['NAME'] . " | " . $bb[$a4][$a5] . "<br />";
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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