The question is simple, let’s suppose that I have an array like:
$array = array(array('bla1' => 'bla1'), array('bla2' => 'bla2'),
array('bla3' => 'bla3'), array('bla4' => 'bla4'));
Yeah it’s multi-dimensional so I need to set value between e.g. array(‘bla1’ => ‘bla1’) and array(‘bla2’ => ‘bla2’) without erasure.
I was puzzled to find, through all array_ like functions in php how to do this.
So, as any other programmer would do, I wrote the function:
function setArrVal($array, $key, $val) {
for ($i = count($array) - 1; $i >= $key; $i--) {
$array[$i + 1] = $array[$i];
}
$array[$key] = $val;
return $array;
}
Works well.
But still need to do this with php function, is there any way to complete this that way?
I’ve tried array_splice($input, 1, 0, $replacement); – worthless, it’s only working with simple values, not with arrays in array.
You can use
array_splice()like so:Demo: http://codepad.org/ivBmZRdn
Is this what you wanted?