0


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

这是想法:

<?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 移动到每第五次迭代。

这一定是结果。

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


我在几周内遇到了麻烦。有没有可能的代码?任何人都可以帮助我吗?

4

3 回答 3

1

这是您的解决方案

<?php
$array  =  array(
                 array('value' => 1),
                 array('value' => 2),
                 array('value' => 2),
                 array('value' => "A"),
                 array('value' => "B"),
                 array('value' => "C"),
                 array('value' => 6),
                 array('value' => 7),
                 array('value' => 7),
                 array('value' => 8),
                 array('value' => 9),
                 array('value' => 10),
                 array('value' => 11)
                );

$required_array = array();
$temp = "";     
foreach($array as $key=>$child) {
    if($child["value"]==$temp) {
        $i = $key+4;
        $required_array[$i] = $child;
    } else {
        $i = $key;
        if(isset($required_array[$key])) $i++;

        $required_array[$i] = $child;   
    }
    $temp = $child["value"];
}
ksort($required_array);

print_r($required_array);
?>

编辑:

以下代码应适用于超过 2 个重复项

$required_array = array();
$temp = "";     
foreach($array as $key=>$child) {
    if($child["value"]==$temp) {
        $i = $key+4;
        $required_array[$i] = $child;
    } else {
        $i = $key;
        //if(isset($required_array[$i])) $i++; // Removed this line
        while(isset($required_array[$i])) { //added this line
            $i++;                           //added this line
        }                                   //added this line

        $required_array[$i] = $child;   
    }
    $temp = $child["value"];
}
ksort($required_array);

print_r($required_array);
于 2013-10-09T11:56:25.517 回答
1

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

$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

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

于 2013-10-09T11:40:42.160 回答
1

这是一个适合您的功能。它查找相同的记录,然后将它们向下移动到正确的位置。

<?php

$array  =  array(
                 array('value' => 1),
                 array('value' => 2),
                 array('value' => 2),
                 array('value' => "A"),
                 array('value' => "B"),
                 array('value' => "C"),
                 array('value' => 6),
                 array('value' => 7),
                 array('value' => 7),
                 array('value' => 8),
                 array('value' => 9),
                 array('value' => 10),
                 array('value' => 11)
                );
function spaceIdenticalRecords(&$array){
        for ($i=1; $i < count($array); $i++) {//loop through the array
                if($array[$i]['value'] === $array[$i - 1]['value']){//look for records that match
                        $replace = array_merge(array_slice($array, ($i+1), 3),array($array[$i]));//move the mathing record down 4
                         array_splice($array, $i, 4, $replace);//place back in the array
                }
        }
}
spaceIdenticalRecords($array)
var_dump($array);
?>

编辑:这个更新的函数应该能够处理任意数量的相同数字。

function spaceIdenticalRecords(&$array){
        for ($i=1; $i < count($array); $i++) {//loop through the array


              if($array[$i]['value'] === $array[$i - 1]['value']){//look for records that match
                        for ($j= $i + 1; $j < count($array); $j++){
                                if($array[$j] !== $array[$i]) break;
                        }


           if ($j+4 > count($array)){
                            $length = count($array) - $j -1;
                    }else{
                            $length = $j - $i + 2;
                    }

                    $replace = array_merge(array_slice($array, $j, 3),array($array[$i]));//move the mathing record down 4
                    for ($k = count($replace); count($replace) < $length +1; $k++){$replace = array_merge($replace,array($array[$i]));}
                    array_splice($array, $i, count($replace), $replace);//place back in the array
                    $i--; //don't miss records!
            }
    }
}
于 2013-10-09T11:51:29.403 回答