1. <i id='66Nva'><tr id='66Nva'><dt id='66Nva'><q id='66Nva'><span id='66Nva'><b id='66Nva'><form id='66Nva'><ins id='66Nva'></ins><ul id='66Nva'></ul><sub id='66Nva'></sub></form><legend id='66Nva'></legend><bdo id='66Nva'><pre id='66Nva'><center id='66Nva'></center></pre></bdo></b><th id='66Nva'></th></span></q></dt></tr></i><div id='66Nva'><tfoot id='66Nva'></tfoot><dl id='66Nva'><fieldset id='66Nva'></fieldset></dl></div>

    <legend id='66Nva'><style id='66Nva'><dir id='66Nva'><q id='66Nva'></q></dir></style></legend>
    • <bdo id='66Nva'></bdo><ul id='66Nva'></ul>
    <tfoot id='66Nva'></tfoot>

      <small id='66Nva'></small><noframes id='66Nva'>

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

      How to count the consecutive duplicate values in an array?(如何计算数组中连续的重复值?)

          <small id='ibnvO'></small><noframes id='ibnvO'>

            <tfoot id='ibnvO'></tfoot>

          1. <i id='ibnvO'><tr id='ibnvO'><dt id='ibnvO'><q id='ibnvO'><span id='ibnvO'><b id='ibnvO'><form id='ibnvO'><ins id='ibnvO'></ins><ul id='ibnvO'></ul><sub id='ibnvO'></sub></form><legend id='ibnvO'></legend><bdo id='ibnvO'><pre id='ibnvO'><center id='ibnvO'></center></pre></bdo></b><th id='ibnvO'></th></span></q></dt></tr></i><div id='ibnvO'><tfoot id='ibnvO'></tfoot><dl id='ibnvO'><fieldset id='ibnvO'></fieldset></dl></div>

            <legend id='ibnvO'><style id='ibnvO'><dir id='ibnvO'><q id='ibnvO'></q></dir></style></legend>
              <tbody id='ibnvO'></tbody>

            • <bdo id='ibnvO'></bdo><ul id='ibnvO'></ul>
              • 本文介绍了如何计算数组中连续的重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个这样的数组:

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

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

                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
                

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

                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;
                

                推荐答案

                只需手动即可:

                $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);
                

                这篇关于如何计算数组中连续的重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                PHP Upload File Validation(PHP 上传文件验证)
                PHP Error - Uploading a file(PHP 错误 - 上传文件)
                How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)
                <i id='Qt7Ch'><tr id='Qt7Ch'><dt id='Qt7Ch'><q id='Qt7Ch'><span id='Qt7Ch'><b id='Qt7Ch'><form id='Qt7Ch'><ins id='Qt7Ch'></ins><ul id='Qt7Ch'></ul><sub id='Qt7Ch'></sub></form><legend id='Qt7Ch'></legend><bdo id='Qt7Ch'><pre id='Qt7Ch'><center id='Qt7Ch'></center></pre></bdo></b><th id='Qt7Ch'></th></span></q></dt></tr></i><div id='Qt7Ch'><tfoot id='Qt7Ch'></tfoot><dl id='Qt7Ch'><fieldset id='Qt7Ch'></fieldset></dl></div>
                      <bdo id='Qt7Ch'></bdo><ul id='Qt7Ch'></ul>

                        <small id='Qt7Ch'></small><noframes id='Qt7Ch'>

                      1. <tfoot id='Qt7Ch'></tfoot>
                          <tbody id='Qt7Ch'></tbody>

                        <legend id='Qt7Ch'><style id='Qt7Ch'><dir id='Qt7Ch'><q id='Qt7Ch'></q></dir></style></legend>