count个元素连续数组php

时间:2015-06-12 15:28:29

标签: php

我有阵列:

         time   Mx            My            Mz           set_nbr   group_min
##1     90.000  0.0105615570  0.0128378518  0.92123599       1         1
##2   4990.909  0.0096134025  0.0117695944  0.78439667       1         1
##3   9891.818  0.0093581318  0.0115742493  0.72867894       1         1
##4  14792.727  0.0031807426  0.0113173105  0.55464140       1         1
##5  19693.636  0.0023569651  0.0089484378  0.42502936       1         1
##6  24594.545  0.0008941874  0.0058824078 -0.04729751       1         1
##7  29495.455 -0.0043247786  0.0021214525 -0.13068103       1         1
##8  34396.364 -0.0070967748  0.0011887832 -0.20001126       1         1
##9  39297.273 -0.0086446611 -0.0009107974 -0.22002091       1         1
##10 44198.182 -0.0087562698 -0.0035490228 -0.30663302       1         1
##11 49099.091 -0.0094095244 -0.0156822550 -0.33245014       1         1
##12 54000.000 -0.0123935570 -0.0190667519 -0.34929570       1         1

        time          Mx            My          Mz         set_nbr group_max
##13     90.000  0.0105615570  0.0128378518  0.92123599       1         3
##14   4990.909  0.0096134025  0.0117695944  0.78439667       1         3
##15   9891.818  0.0093581318  0.0115742493  0.72867894       1         3
##16  14792.727  0.0031807426  0.0113173105  0.55464140       1         3
##17  19693.636  0.0023569651  0.0089484378  0.42502936       1         3
##18  24594.545  0.0008941874  0.0058824078  0.04729751       1         3
##19  29495.455 -0.0043247786  0.0021214525  0.13068103       1         3
##20  34396.364 -0.0070967748  0.0011887832 -0.20001126       1         3
##21  39297.273 -0.0086446611 -0.0009107974 -0.22002091       1         3
##22  44198.182 -0.0087562698 -0.0035490228 -0.30663302       1         3
##23  49099.091 -0.0094095244 -0.0156822550 -0.33245014       1         3
##24  54000.000 -0.0123935570 -0.0190667519 -0.34929570       1         3


 > set_nbr 2   group_min
               group_max       
 > set_nbr 3   group_min
               group_max       
    ..

我打算计算长度大于或等于2的连续链中包含的元素 Rows <- x[ceiling(x$Mz-y$Mz)==0,] 的数量。在示例中,我有三个链,但我只想要计算其中两个。总的来说应该是5。

1 个答案:

答案 0 :(得分:0)

查找&#39; CA&#34;

的连续链
$count = 0;
$link = array();                     // Here will be chain lengths

array_push($types, '');              // to work if the last item == 'CA'

foreach($types as $item) 
  if ($item == 'CA') $count++;       
  else if ($count) 
         { $link[] = $count; $count = 0; }  

rsort($link);                        // 3, 2, 1
$sum = 0;
foreach ($link as $i) 
   if ($i >= 2) $sum += $i;          // Count result
   else break;                       // Further chains are shorter than we want

echo $sum;