当值之间的增量超过 1 时,将具有静态值的元素插入到升序整数的平面数组中

发布于 2024-12-12 03:56:30 字数 682 浏览 0 评论 0原文

我有一个像这样的数组:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 10
    [4] => 11
    [5] => 12
    [6] => 13
    [7] => 14
    [8] => 23
    [9] => 24
    [10] => 25
)

我想填补空白,所以它看起来像这样:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => xxx
    [4] => 10
    [5] => 11
    [6] => 12
    [7] => 13
    [8] => 14
    [9] => xxx
    [10] => 23
    [11] => 24
    [12] => 25
)

如果您查看第一个数组的值,会发现有 1,2,3 ,然后是一个空白然后是10,11,12,13,14,然后是一个间隙,然后是23,24,25。如何以编程方式找到这些间隙并在其位置添加新的数组元素?

最多会有两个间隙。

I have an array like this:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 10
    [4] => 11
    [5] => 12
    [6] => 13
    [7] => 14
    [8] => 23
    [9] => 24
    [10] => 25
)

And I want to fill the gaps so it looks like this:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => xxx
    [4] => 10
    [5] => 11
    [6] => 12
    [7] => 13
    [8] => 14
    [9] => xxx
    [10] => 23
    [11] => 24
    [12] => 25
)

If you look at the values of the first array, there is 1,2,3 and then a gap and then 10,11,12,13,14 and then a gap and then 23,24,25. How can I programmatically find these gaps and add a new array element in its place?

There will be a maximum of two gaps.

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

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

发布评论

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

评论(4

柠檬 2024-12-19 03:56:30

一个简单的 for 循环,不复制数组,而仅更改原始数组:

$repl = 'xxx';

for ($i=1; $i<count($array); $i++) {
    $valueR = $array[$i];
    $valueL = $array[$i-1] === $repl ? $array[$i-2] : $array[$i-1];
    if ($valueR > $valueL + 1) {
        array_splice($array, $i++, 0, $repl);
    }
}

A simple for loop, without copying the array, but only altering the original:

$repl = 'xxx';

for ($i=1; $i<count($array); $i++) {
    $valueR = $array[$i];
    $valueL = $array[$i-1] === $repl ? $array[$i-2] : $array[$i-1];
    if ($valueR > $valueL + 1) {
        array_splice($array, $i++, 0, $repl);
    }
}
屋檐 2024-12-19 03:56:30
$result = array();
if (count($oldArray) > 0)
{
    $result[] = $oldArray[0];
    for ($i=1; $i<count($oldArray); $i++)
    {
         if ($oldArray[$i]-$oldArray[$i-1] != 1)
             $result[] =  "xxx";
         $result[] = $oldArray[$i];
    }
}
$result = array();
if (count($oldArray) > 0)
{
    $result[] = $oldArray[0];
    for ($i=1; $i<count($oldArray); $i++)
    {
         if ($oldArray[$i]-$oldArray[$i-1] != 1)
             $result[] =  "xxx";
         $result[] = $oldArray[$i];
    }
}
清眉祭 2024-12-19 03:56:30

我会做这样的事情,未经测试但应该有效:)

$oldArray = array(1,2,3,10,11,12,13,24,25,26,27);

$newArray = array();
for($i=0;$i<count($oldArray);$i++){
    $newArray[] = $oldArray[$i];
    if($oldArray[$i+1] - $oldArray[$i] != 1 && $i+1 != count($oldArray))
        $newArray[] = "xxx"; // seperator
}

var_dump($newArray);

Shai

I would do something like this, not tested but should work :)

$oldArray = array(1,2,3,10,11,12,13,24,25,26,27);

$newArray = array();
for($i=0;$i<count($oldArray);$i++){
    $newArray[] = $oldArray[$i];
    if($oldArray[$i+1] - $oldArray[$i] != 1 && $i+1 != count($oldArray))
        $newArray[] = "xxx"; // seperator
}

var_dump($newArray);

Shai

疯到世界奔溃 2024-12-19 03:56:30

我将修改输入数组(如 @netcoder 所做的那样),而不是通过填充新的扩展数组来使内存加倍。

foreach() 不适合与 array_splice() 一起使用,因为会在迭代时弄乱索引。

为了稳定性,请使用 for() 循环。要最大限度地减少 count() 的调用,请仅在循环开始时调用一次,然后仅在必要时增加缓存的计数。

由于第一个元素永远没有资格注入新元素,因此从索引 1 开始循环。

如果前一个元素的值+1等于当前元素的值,则将该位置的元素注入到原始数组中。注入后,改变 $i$count 变量,以便循环执行额外的迭代,但不会访问注入的元素。

代码:(演示

$array = [-5, 1, 2, 3, 10, 11, 12, 13, 14, 23, 24, 25, 31];
for ($i = 1, $count = count($array); $i < $count; ++$i) {
    if (($array[$i - 1] + 1) != $array[$i]) {
        array_splice($array, $i++, 0, 'xxx');
        ++$count;
    }
}
var_export($array);

输出:

array (
  0 => -5,
  1 => 'xxx',
  2 => 1,
  3 => 2,
  4 => 3,
  5 => 'xxx',
  6 => 10,
  7 => 11,
  8 => 12,
  9 => 13,
  10 => 14,
  11 => 'xxx',
  12 => 23,
  13 => 24,
  14 => 25,
  15 => 'xxx',
  16 => 31,
)

Rather than doubling the memory by populating a new, extended array, I'll modify the input array (as @netcoder did).

foreach() is unsuitable to use with array_splice() because will foul up the indexes while you iterate.

For stability, use a for() loop. To minimize calls of count(), only call it once at the start of the loop, then bump the cached count only when necessary.

Because the first element will never qualify for the injection of the new element, start the loop at index 1.

If the the previous element's value + 1 is equal to the current element's value, then inject the element at that position into the original array. After the injection, bump the $i and $count variables so that the loop performs an additional iteration, but doesn't bother visiting the injected element.

Code: (Demo)

$array = [-5, 1, 2, 3, 10, 11, 12, 13, 14, 23, 24, 25, 31];
for ($i = 1, $count = count($array); $i < $count; ++$i) {
    if (($array[$i - 1] + 1) != $array[$i]) {
        array_splice($array, $i++, 0, 'xxx');
        ++$count;
    }
}
var_export($array);

Output:

array (
  0 => -5,
  1 => 'xxx',
  2 => 1,
  3 => 2,
  4 => 3,
  5 => 'xxx',
  6 => 10,
  7 => 11,
  8 => 12,
  9 => 13,
  10 => 14,
  11 => 'xxx',
  12 => 23,
  13 => 24,
  14 => 25,
  15 => 'xxx',
  16 => 31,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文