Code: Select all
function countMaxOcc($tpp, $s){
$t=0;$c=0;$p=true;
foreach($tpp as $n){
if($n==$s){
$c=($p)?$c+1:1;
$p=true;
}else{
$t=($c>$t)?$c:$t;
$p=false;
}
}
return $t;
}Moderator: General Moderators
Code: Select all
function countMaxOcc($tpp, $s){
$t=0;$c=0;$p=true;
foreach($tpp as $n){
if($n==$s){
$c=($p)?$c+1:1;
$p=true;
}else{
$t=($c>$t)?$c:$t;
$p=false;
}
}
return $t;
}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;
}Code: Select all
$c=($p)?$c+1:1;It's a ternary operator. Could also be written as such:kcp88 wrote:Thanks a lot !I think it is correct after i run several tries . BTW I don't understand what is the "?" and "1:1" doing ... , as in below code .
Code: Select all
$c=($p)?$c+1:1;
Code: Select all
if ($p)
{
$c += 1;
}
else
{
$c = 1;
}