如何统计数组中连续重复的值?

发布于 2024-10-21 07:16:43 字数 449 浏览 2 评论 0原文

我有一个像这样的数组:

$arr = array(1, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3);

我找到了函数 array_count_values(),但它将对所有相同的值进行分组并计算出现次数,而不考虑连续序列中的中断。

$result[1] = 5
$result[2] = 4
$result[3] = 3

如何对每组连续值进行分组并计算每个序列的长度?请注意,数字 123 有两组序列。

我期望生成的数据需要类似于以下内容:

[1] = 3;
[2] = 2;
[3] = 2;
[1] = 2;
[2] = 2;
[3] = 1;

I have an array like this:

$arr = array(1, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3);

I found the function array_count_values(), but it will group all of the same values and count the occurrences without respecting breaks in the consecutive sequences.

$result[1] = 5
$result[2] = 4
$result[3] = 3

How can I group each set of consecutive values and count the length of each sequence? Notice there are two sets of sequences for the numbers 1, 2, and 3.

The data that I expect to generate needs to resemble this:

[1] = 3;
[2] = 2;
[3] = 2;
[1] = 2;
[2] = 2;
[3] = 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

把人绕傻吧 2024-10-28 07:16:44

可以简单地手动完成:

$arr = array(1,1,1,2,2,3,3,1,1,2,2,3);

$result = array();
$prev_value = array('value' => null, 'amount' => null);

foreach ($arr as $val) {
    if ($prev_value['value'] != $val) {
        unset($prev_value);
        $prev_value = array('value' => $val, 'amount' => 0);
        $result[] =& $prev_value;
    }

    $prev_value['amount']++;
}

var_dump($result);

It can be done simply manually:

$arr = array(1,1,1,2,2,3,3,1,1,2,2,3);

$result = array();
$prev_value = array('value' => null, 'amount' => null);

foreach ($arr as $val) {
    if ($prev_value['value'] != $val) {
        unset($prev_value);
        $prev_value = array('value' => $val, 'amount' => 0);
        $result[] =& $prev_value;
    }

    $prev_value['amount']++;
}

var_dump($result);
遥远的绿洲 2024-10-28 07:16:44

PHP 的 array_count_values 函数怎么样?

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

输出:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

What about PHP's array_count_values function?

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

output:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)
我家小可爱 2024-10-28 07:16:44

我的建议是在进入循环之前从数组中提取并删除第一个值,并使用临时数组 ($carry) 来跟踪每个新值是否与进位数组中的键匹配。如果是这样,请增加它。如果没有,则将已完成的序列计数推送到结果数组中,并用新值覆盖进位并将计数器设置为 1。当循环结束时,将延迟进位推送到结果集中。我的代码片段不检查输入数组是否为空;如有必要,请将该条件添加到您的项目中。

代码:(演示)

$array = [1,1,1,2,2,3,3,1,1,2,2,3];
 
$result = [];
$carry = [array_shift($array) => 1];
 
foreach ($array as $value) {
    if (isset($carry[$value])) {
        ++$carry[$value];
    } else {
        $result[] = $carry;
        $carry = [$value => 1];
    }
}
$result[] = $carry;
print_r($result);

输出:(压缩以减少页面膨胀)

[
    [1 => 3],
    [2 => 2],
    [3 => 2],
    [1 => 2],
    [2 => 2],
    [3 => 1],
]

如果您更愿意实现 zerkms 风格,通过引用修改样式技术,以下代码片段提供与上述代码片段相同的结果。

实际上,它将每个新遇到的值作为关联的单元素数组推送到索引结果数组中。由于推送的子数组被声明为变量 ($carry),然后通过引用 (= &) 分配给结果数组,因此 $carry 递增 将应用于结果数组中的深层嵌套值。输出数组在其结构中需要额外的深度,以便可以可靠地存储多次出现的给定值。

代码:(Demo)

$result = [];
$carry = [];
foreach ($array as $value) {
    if ($carry && key($carry) === $value) {
        ++$carry[$value];
    } else {
        unset($carry);
        $carry = [$value => 1];
        $result[] = &$carry;
    }
}
unset($carry);
print_r($result);

循环后取消设置引用变量$carry可能不会必要的,但如果在变量的作用域内可能会重复使用该变量,则使用 unset() 取消引用将很重要。

只是为了好玩,这里有一个可怕的正则表达式注入方法,适用于示例数据:演示

My suggestion is to extract&remove the first value from the array prior to entering the loop and use a temporary array ($carry) to track whether each new value matches the key in the carry array. If so, increment it. If not, push the completed sequence count into the result array and overwrite the carry with the new value and set the counter to 1. When the loop finishes, push the lingering carry into the result set. My snippet does not check if the input array is empty; if necessary, add that condition to your project.

Code: (Demo)

$array = [1,1,1,2,2,3,3,1,1,2,2,3];
 
$result = [];
$carry = [array_shift($array) => 1];
 
foreach ($array as $value) {
    if (isset($carry[$value])) {
        ++$carry[$value];
    } else {
        $result[] = $carry;
        $carry = [$value => 1];
    }
}
$result[] = $carry;
print_r($result);

Output: (condensed to reduce page bloat)

[
    [1 => 3],
    [2 => 2],
    [3 => 2],
    [1 => 2],
    [2 => 2],
    [3 => 1],
]

If you'd rather implement a zerkms-style, modify-by-reference style technique, the following snippet provides the same result as the above snippet.

Effectively, it pushes every newly encountered value as an associative, single-element array into the indexed result array. Because the pushed subarray is declared as a variable ($carry) then assigned-by-reference (= &) to the result array, incrementation of $carry will be applied to the deeply nested value in the result array. The output array requires the additional depth in its structure so that a given value which occurs multiple times can be reliably stored.

Code: (Demo)

$result = [];
$carry = [];
foreach ($array as $value) {
    if ($carry && key($carry) === $value) {
        ++$carry[$value];
    } else {
        unset($carry);
        $carry = [$value => 1];
        $result[] = &$carry;
    }
}
unset($carry);
print_r($result);

Unsetting the reference variable $carry after the loop may not be necessary, but if there is any potential re-use of that variable within the variable's scope, it will be important to uncouple the reference with unset().

And just for fun, here is a hideous regex-infused approach that works with the sample data: Demo

╭ゆ眷念 2024-10-28 07:16:44
function findRepetitions($times, $array) {

    $values = array_unique($array);

    $counts = [];
    foreach($values as $value) {
        $counts[] = ['value' => $value, 'count' => 0];
    }

    foreach ($array as $value) {
        foreach ($counts as $key => $count) {
            if ($count['value'] === $value) {
                $counts[$key]['count']++;
            }
        }
    }

    $repetitions = [];
    foreach ($counts as $count) {
        if ($count['count'] === $times) {
            $repetitions[] = $count['value'];
        }
    }

    return $repetitions;
}
function findRepetitions($times, $array) {

    $values = array_unique($array);

    $counts = [];
    foreach($values as $value) {
        $counts[] = ['value' => $value, 'count' => 0];
    }

    foreach ($array as $value) {
        foreach ($counts as $key => $count) {
            if ($count['value'] === $value) {
                $counts[$key]['count']++;
            }
        }
    }

    $repetitions = [];
    foreach ($counts as $count) {
        if ($count['count'] === $times) {
            $repetitions[] = $count['value'];
        }
    }

    return $repetitions;
}
归属感 2024-10-28 07:16:44
$current = null;
foreach($your_array as $v) {
    if($v == $current) {
        $result[count($result)-1]++;
    } else {
        $result[] = 1;
        $current = $v;
    }
}

var_dump($result);
$current = null;
foreach($your_array as $v) {
    if($v == $current) {
        $result[count($result)-1]++;
    } else {
        $result[] = 1;
        $current = $v;
    }
}

var_dump($result);
哑剧 2024-10-28 07:16:44

这是我的做法:

function SplitIntoGroups($array)
{
    $toReturnArray = array();
    $currentNumber = $array[0];
    $currentCount = 1;
    for($i=1; $i <= count($array); $i++)
    {
        if($array[$i] == $currentNumber)
        {
            $currentCount++;
        }
        else
        {
            $toReturnArray[] = array($currentNumber, $currentCount);
            $currentNumber = $array[$i];
            $currentCount = 1;
        }
    }

    return $toReturnArray;
}

$answer = SplitIntoGroups(array(1,1,1,2,2,3,3,1,1,2,2,3));
for($i=0; $i<count($answer); $i++)
{
    echo '[' . $answer[$i][0] . '] = ' . $answer[$i][1] . '<br />';
}

Here is the way that I would do it:

function SplitIntoGroups($array)
{
    $toReturnArray = array();
    $currentNumber = $array[0];
    $currentCount = 1;
    for($i=1; $i <= count($array); $i++)
    {
        if($array[$i] == $currentNumber)
        {
            $currentCount++;
        }
        else
        {
            $toReturnArray[] = array($currentNumber, $currentCount);
            $currentNumber = $array[$i];
            $currentCount = 1;
        }
    }

    return $toReturnArray;
}

$answer = SplitIntoGroups(array(1,1,1,2,2,3,3,1,1,2,2,3));
for($i=0; $i<count($answer); $i++)
{
    echo '[' . $answer[$i][0] . '] = ' . $answer[$i][1] . '<br />';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文