Inserting Multiple Items Into Array At Key Positions

  arrays, php

In my cart class I have a loop:

foreach($this->items as $key => $item) {
    $duplicate = clone $item;
    $duplicate->price = 0;
    $duplicate->dynamic = 1;
    // Duplicate new item to cart
    $this->AddItem($duplicate, $key+1);

Over in the AddItem function it does this:
array_splice($this->items, $position, 0, array($newItem));

And it works, but the problem is the items are not going in the place I want them too. This is going to be tricky to explain but hopefully someone can understand.

So lets say for example the $items array is made up of:
array('a', 'b', 'c', 'd')

I end up with:

array('a', 'a2', 'b2', 'c2', 'b', 'c', 'd')

But what I want is:
array('a', 'a2', 'b', 'b2', 'c', 'c2', 'd')

Because the $key value is not changing within the foreach loop, it inserts it into the position $key of the old $this->items array. But I want the new items to be duplicated after their original counterparts. I hope this makes sense.

Source: Ask PHP