How to add an array value to the middle of an array?

Viewed 15950

Lets say I have this array:

$array = array(1,2,'b','c',5,6,7,8,9.10);

Later in the script, I want to add the value 'd' before 'c'. How can I do this?

4 Answers

Use array_splice as following:

array_splice($array, 3, 0, array('d'));

or a more self-made approach: Loop array until you see 'd' insert 'c' then 'd' in the next one. Shift all other entries right by one

Related