fork download
  1. <?php
  2.  
  3. $myArr = [0 => 0, 1 => 0, 2 => 0, 3 => 1, 4 => 1, 5 => 0, 6 => 0];
  4.  
  5. $counter = 0;
  6. $in_group = false;
  7. $arrayGroups = [];
  8. foreach ($myArr as $val) {
  9. if ($val == 0) {
  10. if ($in_group) {
  11. $arrayGroups[$counter]++;
  12. } else {
  13. $in_group = true;
  14. $counter++;
  15. $arrayGroups[$counter] = 1;
  16. }
  17. } else {
  18. $in_group = false;
  19. }
  20. }
  21.  
  22. print_r($arrayGroups);
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
Array
(
    [1] => 3
    [2] => 2
)