2016-03-31 40 views
0

我需要查找大于特定浮点数的记录数并找到重复最多的数据组。例如,我有下面的数据和予需要找到条目多少具有值> 4.使用PHP识别重复模式

1.5 
    1.7 
    4.5 
    4.7 
    4.8 
    1.4 
    4.5 
    4.9 

在上述数据值大于4是4.5,4.7,4.8更大最长连续重复。因此,我想要返回的总数应该是3.正如你可以看到该模式在4.8之后破坏,因为数字是1.4以上。有没有一种方法来识别这种模式?

+0

是一个数组 – Sinto

+0

中值做你尝试什么? – user3470953

+0

你应该至少试图自己解决这个问题。你到目前为止尝试过哪些解决方案? – Styphon

回答

0

试试这个,我在这里使用了数组:

$arr = array(
     0 => '1.5', 
     1 => '1.7', 
     2 => '4.5', 
     3 => '4.7', 
     4 => '4.8', 
     5 => '1.4', 
     6 => '4.5', 
     7 => '4.9' 
    ); 
     $chk_val = 4; // value which is checking 
     $cnt = 0;$inc = 0; 
     foreach ($arr as $i => $val) { 
     if ($val > $chk_val) { 
      $inc++; 
      if ($inc > $cnt) { $cnt = $inc;} 
     } else { 
      $inc = 0; 
     } 
     } 
     echo $cnt; 
0

试试这个

$n = 4; // number to check 
$count = 0; 
$max = 0; 
$ele = array(1.5, 1.7, 4.5, 4.7, 4.8, 1.4, 4.5, 4.9); 
for ($i = 0; $i < count($ele); $i++) { 
    if ($ele[$i] >= $n) { // check for greater element than given number 
    $count++; // increase consecutive counter variable 
    $arr[$max] = $count; //save continues max counter to array 
    } else { 
    $count = 0; //reset consecutive counter 
    $max++; 
    }  
} 
echo max($arr); 
0

快速和肮脏......

function findNums($nums, $min = 4) { 
    $groups = array(); 
    $groupcounts = array(); 
    $groupindex = 0; 
    foreach($nums as $num) { 
    if($num > $min) { 
     $groups[$groupindex][] = $num; 
     if(array_key_exists($groupindex, $groupcounts)) { 
     $groupcounts[$groupindex]++; 
     } else { 
     $groupcounts[$groupindex] = 1; 
     } 
    } else { 
     $groupindex++; 
    } 
    } 
    return array($groupcounts, $groups); 
} 

// $your_numbers is your list 
$nums = array_map('trim', explode("\n", $your_numbers)); 
$result = findNums($nums); 
$counts = $result[0]; 
$maxcount = max($counts); 
$groups = $result[1]; 
echo "max count is ".$maxcount." with values:\n"; 
$key = array_search($maxcount, $counts); 
var_dump($groups[$key]);