I would like to insert two arrays after a particular index matches a condition in a multidimensional array while in a foreach loop and remove the existing array at that index
Sample array:
$array = array(
array( 1, 2, 3 ),
array( 4, 5, 6 ),
array( 7, 8, 9 )
);
After inserting and removing existing index array
$array = array(
array( 1, 2, 3 ),
array( 'new', 'array' ),
array( 'another', 'item' ),
array( 7, 8, 9 )
);
While in a loop when a condition is met so:
foreach ( $array as $index => $value ) :
if ( $index % 2 == 0 ) :
// insert two array elements in $array after index $index and remove $index
endif;
endforeach;
Any help is appreciated.
UPDATE:
I've tried array_splice() but because it increases the length of the existing array on a match, it ends up doing overriding the new array elements in the loop as well.