0

I need to count consecutive repeated elements in the array, but if they are non consecutive I want to count them also, to have them in the same sequence as they are.

For example I have flowing array:

$array = Array('a', 'b', 'a', 'a', 'a', 'c', 'c', 'b', 'b', 'a', 'a', 'a', 'a');

if I use array_count_values function example:

print_r(array_count_values($array));

Output will be:

Array ( [a] => 8 [b] => 3 [c] => 2 )

But I need to get output like this:

element   counted value
    a       1
    b       1
    a       3
    c       2
    b       2
    a       4
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

4 Answers4

4

You can iterate over the array, counting values while they are the same, and pushing a value to the result array when they are different:

$result = [];
$count = 0;
$current = $array[0];
for ($i = 0; $i < count($array); $i++) {
    if ($array[$i] == $current) {
        $count++;
    }
    else {
        $result[] = array($current, $count);
        $count = 1;
    }
    $current = $array[$i];
}
$result[] = array($current, $count);
print_r($result);

Output:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => 1
        )
    [1] => Array
        (
            [0] => b
            [1] => 1
        )
    [2] => Array
        (
            [0] => a
            [1] => 3
        )
    [3] => Array
        (
            [0] => c
            [1] => 2
        )
    [4] => Array
        (
            [0] => b
            [1] => 2
        )
    [5] => Array
        (
            [0] => a
            [1] => 4
        )
)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
2

Starting with an array with the first item from the source (and a count of 1), this then starts from the second item and checks if it matches the one in the current output. Adding 1 if it matches or adding a new entry if it doesn't...

$array = Array ('a','b','a','a','a','c','c','b','b','a','a','a','a');
$output = [ ["element" => $array[0], "count" => 1 ] ];
$outputPoint = 0;
for ( $i = 1, $count = count($array); $i < $count; $i++ )   {
    if ( $array[$i] == $output[$outputPoint]["element"] )  {
        $output[$outputPoint]["count"]++;
    }
    else    {
        $output[++$outputPoint] = ["element" => $array[$i], "count" => 1 ];
    }
}
print_r($output);

Which outputs...

Array
(
    [0] => Array
        (
            [element] => a
            [count] => 1
        )

    [1] => Array
        (
            [element] => b
            [count] => 1
        )

    [2] => Array
        (
            [element] => a
            [count] => 3
        )

    [3] => Array
        (
            [element] => c
            [count] => 2
        )

    [4] => Array
        (
            [element] => b
            [count] => 2
        )

    [5] => Array
        (
            [element] => a
            [count] => 4
        )

)
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
2

This loops through the array and increments the number of elements in $count.

$array = ['a','b','a','a','a','c','c','b','b','a','a','a','a'];

$count = [];
$crt   = -1;
foreach($array ?? [] as $element) {
    if($crt == -1 || $count[$crt]["element"] != $element) {
        $count[++$crt] = ["element" => $element, "counted_value" => 1];
    } else {
        $count[$crt]["counted_value"]++; 
    }
}

The array looks like:

Array
(
    [0] => Array
        (
            [element] => a
            [counted_value] => 1
        )

    [1] => Array
        (
            [element] => b
            [counted_value] => 1
        )

    [2] => Array
        (
            [element] => a
            [counted_value] => 3
        )

    [3] => Array
        (
            [element] => c
            [counted_value] => 2
        )

    [4] => Array
        (
            [element] => b
            [counted_value] => 2
        )

    [5] => Array
        (
            [element] => a
            [counted_value] => 4
        )

)
0

Compare 2 elements by iterating the array, if both the elements are the same, increase the count else keep the count as 1

$index = 0;
$finalArr[$index] = array('ele'=>$array[0],'cnt'=>1);

for($i=1; $i<count($array); $i++)
{
   if($array[$i]===$array[$i-1])
   {
      ++$finalArr[$index]['cnt'];
   }
   else
   {
      $finalArr[++$index] = array('ele'=>$array[$i],'cnt'=>1);
   }
}
print_r($finalArr);
hruthi
  • 9
  • 3