array_count_values
won't help you with this if keeping track of consecutive repetitions is important. You'll just have to loop over the array and compare each value to the previous value. If it matches, increment your "repeated" count for that word.
$origen = array("dog","dog","dog","cat","cat","bear","bear","alien","mouse");
$previous = null;
$repetitions = array();
foreach ($origen as $word) {
if ($word == $previous) {
if (!isset($repetitions[$word])) $repetitions[$word] = 1;
$repetitions[$word]++;
}
$previous = $word;
}
print_r($repetitions);
Output
Array ( [dog] => 3 [cat] => 2 [bear] => 2 )
Your comment was correct, the previous method only works if there is only one repeated set of any repeated words. If there can be more than one set (why now?) I came up with this:
First, separate the array of words into sub-arrays, separated by repeated sets.
$previous = null;
foreach ($origen as $word) {
if ($word != $previous) {
if (!empty($set)) $sets[] = $set;
$set = [$word];
} else {
$set[] = $word;
}
$previous = $word;
}
if ($set) $sets[] = $set;
Then examine the sets and add up instances that have more than one item (consecutive repetitions of a word.)
foreach ($sets as $set) {
if (count($set) > 1) {
$key = reset($set);
if (!isset($result[$key])) $result[$key] = 0;
$result[$key] += count($set);
}
}