<?php
class Test {
private $items = ['a','b','c','d'];
public function dup()
{
$dupes = 0;
foreach ($this->items as $key => $item) {
$this->addItem($item . '2', $key + 1 + $dupes);
$dupes++;
}
}
private function addItem($newItem, $position) {
array_splice($this->items, $position, 0, array($newItem));
}
public function dump()
{
var_dump($this->items);
}
}
$test = new Test();
$test->dup();
$test->dump();
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array(8) {
[0]=>
string(1) "a"
[1]=>
string(2) "a2"
[2]=>
string(1) "b"
[3]=>
string(2) "b2"
[4]=>
string(1) "c"
[5]=>
string(2) "c2"
[6]=>
string(1) "d"
[7]=>
string(2) "d2"
}
preferences:
134.63 ms | 407 KiB | 5 Q