(Disclaimer, I'm pretty tired right now, so I could of missed something...
Looks to be a function that will go through an array, and return the maximum number of times that what you are searching for occur next to each other. (see my final notes though...)
ie, passed the array:
c,a,
b,
b,
b,d,a,e,
b,
b,e,e,f,g,
b,
b,
b,
b,
b,
b,
b,
b,e,a,c,
b,
b,
b,d,d,c,c
and you pass it you are checking 'b', you would get 8 for the group where 'b' was 8 times in a row.
Here is the same function, written to explain more:
Code: Select all
function countMaxOcc($arySearch, $strMatch) {
$intMaxTotal = 0; // ($t) contains the highest count found
$intCurCount = 0; // ($c) Conatins the current count for this matching group
$bInGroup = TRUE; // ($p) Says you are current in a group (ie, last item also matched)
foreach($arySearch as $strItem) {
if ($strItem == $strMatch) {
// This item matched what we are looking for
if ($bInGroup) { // ie. did the last item also match?
$intCurCount++;
}
else { // ie. Last item didn't match, so we start at 1
$intCurCount = 1;
}
// Lets the next loop know we matched here, so keep increasing $intCurCount
$bInGroup = TRUE;
}
else {
// This item doesn't match what we are looking for
if ($intCurCount > $intMaxTotal) {
// The last group's total was higher than any other so far
$intMaxTotal = $intCurCount;
}
else {
// As a broken out IF, this really isn't needed, but put in to equate other code
$intMaxTotal = $intMaxTotal;
}
// Lets the next loop know the this one didn't match (so $intCurCount restarts at 1)
$bInGroup = FALSE;
}
}
return $intMaxTotal;
}
My final notes, if I am correct at what this function is supposed to be doing, it is doing it WRONG. The way it is coded, if you pass it this:
a,
b,
b,
b,c,d,e,
b,
b,
b,
b,a,c,d,e,
b,
b,
b,
b,
b,
b,
b
What do you think you would get? If you are looking for the maximum number of times B occurs in a row, the answer should be 7 for the last group. However the way it is programmed, you would actually get 4, because the loop ends on a match, so it never hits the code to assign
$c (
$intCurCount in my version) to
$t (
$intMaxTotal) before exiting the loop and returning it.
-Greg