Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Suppose I have the following array $diff.

a, a, a, a, b, b, b, a, a, b, b, b, a, a, a, b

A represents a value inside $diff.
B represents an Array inside $diff.

Now I have to count A if it occurred more than two times in its sequence and is not an Array (instead a value). Otherwise, disregard it.

For the above input, the code should function as follows

[a] = not an array; 0
[a,a] = not an array; 0
[a,a,a] = not an array; 3
[a,a,a,a] = not an array; 4
[b] = array;
[b,b] = array;
[b,b,b] = array;
[a] = not an array; 0
[a,a] = not an array; 0
[b] = array;
[b,b] = array;
[b,b,b] = array;
[a] = not an array; 0
[a,a] = not an array; 0
[a,a,a] = not an array; 3
[b] = array;

Here is my attempt, but it doesn't work!, the value gets changed because the values get replaced.

<?php

foreach($diff as $key => $val)  {

    if (!is_array($diff[$key])) { // THIS MEANS THAT THE CURRENT ELEMENT IS NOT AN ARRAY. 
       if(is_array($diff[$key-1]) ) {   //START OF SEQ. IF THE PREVIOUS ELEMENT IS AN ARRAY AND CURRENT ELEMENT IS NOT AN ARRAY.

        $SEQ_START=$key;
        $n=1;

            for($i=0; $i<=count($diff); $i+=1) { // I AM CHECKING HERE IF THE NEXT 3 ELEMENTS are NOT ARRAY, HENCE I CAN INCREMENT IT

            if(!is_array($diff[$SEQ_START+$i])) $n+=1;
            else $n=0;
            }
        }
    }
}

?>
share|improve this question
    
I understand the logic of the count, but what is the end result you are looking for? The number of contiguous segments (> 2), or the number of elements in the contiguous segments (> 2)? (e.x. if the former, the answer would be 2, if the latter, the answer would be 7) –  Grexis Dec 22 '11 at 10:06
    
I need the answer to be 7 in the above example. –  SupaOden Dec 22 '11 at 10:12

3 Answers 3

up vote 3 down vote accepted

UPDATED as per @Grexis' comment below

$diff = array(array(),'a', 'a', 'a', 'a', array(), array(), array(), 'a', 'a', array(), array(), array(), 'a', 'a', 'a', array());

// Counter to hold current sequence total
$count = 0;
// Array to hold results
$counted = array();

// Loop array
foreach ($diff as $key => $val) {
  if (is_array($val)) { // If it is an array
    if ($count > 2) { // If the counter is more than 2
      $counted[(isset($seq)) ? $seq + 1 : 0] = $count; // add it to the array
    }
    // Reset the counter
    $count = 0;
    $seq = $key;
  } else {
    // Increment the counter
    $count++;
  }
} 
// If there is a >2 counter at the end, add one more result
if ($count > 2) {
  $counted[(isset($seq)) ? $seq + 1 : 0] = $count;
}

print_r($counted);
// Outputs:
// Array
// (
//     [0] => 4
//     [12] => 3
// )

// or if you want the total count
$total = array_sum($counted);
echo $total; // 7

See it working

share|improve this answer
    
It's unnecessary, but if you include the index in the foreach, you could store the index of the start of the sequence as the index in $counted. Something like $counted[$index-$count] = $count; on line 10 and $counted[count($diff)-$count] = $count; on line 21 –  Grexis Dec 22 '11 at 10:20
    
@Grexis I updated the code with how I would do that :-D –  DaveRandom Dec 22 '11 at 10:26
    
Not gonna lie, your implementation is definitely neater... showoff :p –  Grexis Dec 22 '11 at 10:35

All you need is a counter that counts the consecutive non-array values. Increase it with every array value and reset it with every non-array value:

$seqLength = 0;
foreach ($arr as $index => $value) {
    if (is_array($value)) {
        $seqLength++;
        echo 'array';
    } else {
        $seqLength = 0;
        echo 'not an array';
    }
    if ($seqLength > 2) {
        echo '; '.$seqLength;
    } else {
        echo '; 0';
    }
}
share|improve this answer

maybe:

$a = array(1,1,1,1,1,array(1,1),1,1,1,1,1,array(1,1));
$i = 0;
$j = 0;
foreach ($a as $item){
    if ($i>=2){
        $j=$i + 1;
    }
    if (!is_array($item)){
        $i++;
        echo $j.'<br>';
    }else{
        $i=0;
        $j=0;
        echo '<br>';
    }   
}

output:

0
0
3
4
5

0
0
3
4
5
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.