I want to shift an array by N from O, I mean, I want to insert N new values from O offset, and preserve the original array.
I took a look to array_splice
, and in the example of php doc, they do what I want but it doesn't work for me. Here is my code :
function arrayShift(array $array, int $offset, int $length) {
$insert = [];
// create new array of $length size
for ($i = 0; $i < $length; $i++) {
$insert []= '0';
}
return array_splice($array, $offset, 0, $insert);
}
// my array is multidimensionnal with 16 values
$array = [
0 => [
'value1' => 'test',
],
.....
];
var_dump(arrayShift($array, 5, 2));
// it returns empty array
I want this array :
array:10 [
0 => array:3 [
"type" => "choices"
"choices" => array:1 []
"field" => array:3 []
]
1 => array:3 []
2 => array:3 []
3 => array:3 []
4 => array:3 []
5 => array:3 []
6 => array:3 []
7 => array:3 []
8 => array:3 []
9 => array:3 []
]
To become this array :
array:12 [
0 => array:3 [
"type" => "choices"
"choices" => array:1 []
"field" => array:3 []
]
1 => array:3 []
2 => array:3 []
3 => array:3 []
4 => array:3 []
5 => array:3 []
6 => '0',
7 => '0',
8 => array:3 []
9 => array:3 []
10 => array:3 []
11 => array:3 []
]
Is it because it's a multidimensional array ? I don't think it changes something.