首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >php order by sequence多维数组每五次迭代

php order by sequence多维数组每五次迭代
EN

Stack Overflow用户
提问于 2013-10-09 19:13:58
回答 3查看 126关注 0票数 0

我想要有一个数组,如果它有重复的话,它会将索引和值移动到第五个计数。并使用该字段值,而不管该值是整数还是字母数字。

想法是这样的:

代码语言:javascript
复制
<?php

$array  =  array(
                 array('value' => 1),
                 array('value' => **2**),
                 array('value' => **2**), //Move this
                 array('value' => **2**), //Move the also second duplicate
                 array('value' => A),
                 array('value' => B),
                 array('value' => C),
                 array('value' => 6),
                 array('value' => **7**),
                 array('value' => **7**), //Move this
                 array('value' => 8),
                 array('value' => 9),
                 array('value' => 10),
                 array('value' => 11)
                );
?>

我希望将值5和7移动到每五次迭代。

这一定是结果。

代码语言:javascript
复制
<?php

$array  =  array(
                 array('value' => 1),
                 array('value' => **2**),
                 array('value' => A),
                 array('value' => B),
                 array('value' => C),
                 array('value' => **2**), //Move to fifth array, row
                 array('value' => 6),
                 array('value' => **7**),
                 array('value' => 8),
                 array('value' => **2**), //Move to the fifth array if has second duplicate
                 array('value' => 9),
                 array('value' => 10),
                 array('value' => **7**), //Move to fifth array, row
                 array('value' => 11)
                );
?>

我这几个星期都遇到麻烦了。有没有可能的代码呢?有人能帮我吗?

EN

Stack Overflow用户

发布于 2013-10-09 19:40:42

我不确定我是否100%理解了这个问题,但以下代码会产生所需的输出:

代码语言:javascript
复制
$lastvalue= null;
$toinsert= null;
$insertcount= 0;
$insertindex= -1;
$result= array();

for($ndx=0;$ndx<count($array);$ndx++)
{
    $value= $array[$ndx]['value'];
    if ($value==$lastvalue) 
    {
        // It's a duplicate, set the insert index
        $insertindex= $ndx+4; // 4 places on from here (5 from the first occurrence)
        // Store the value we want to duplicate
        $toinsert= $array[$ndx];
        $insertcount++;
    }
    elseif(isset($toinsert) && $value==$toinsert['value']) {
        // We're already dealing with one of these, add this to the count to be
        // inserted
        $insertcount++;
    }
    else 
    {
        if ($ndx==$insertindex)
        {
            // We've reached the insertindex, insert our duplicate(s)
            for($i=0;$i<$insertcount;$i++)
            {
                $result[]= $toinsert;
            }
            $insertcount= 0;
        }
        $result[]= $array[$ndx];
        $lastvalue= $value; // Store this value so we can check for duplicates in the next iteration
    }
}

print_r($result);

基本上,我所做的就是遍历原始数组,查找副本。如果发现重复的值,它会记下重复的值,并计算应该插入该值的索引(当前索引+4-从重复的原始出现处开始迭代5次!)。当它到达该索引时,它会将存储的值插入到结果中。

注意:如果在找到第一个重复项的插入点之前找到第二组重复项,这将不起作用(它将放弃第一个重复项,并使用第二个重复项)。如果您在到达插入点之前到达原始数组的末尾,它也不会重新插入副本。

这真的有帮助吗?

编辑已更新,可以处理多个连续的重复项。它通过对命中$insertindex时需要插入的记录数进行计数来实现这一点。

注意:这段代码现在有点难看了-它很可能是已经建议的其他答案之一,更好地适应了多个重复的场景。

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19270488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档