I have a multidimensional array that is built like this (don't mind the string lengths, I modified the values)
array(27) {
[0]=>
array(2) {
["name"]=>
string(16) "Arsenal FC"
["id"]=>
string(2) "1"
}
[1]=>
array(2) {
["name"]=>
string(8) "Liverpool FC"
["id"]=>
string(3) "2"
}
[2]=>
array(2) {
["name"]=>
string(18) "Manchester United"
["id"]=>
string(3) "3"
}
}
Now I want to add an array, that is built like this
array(2) {
["name"]=>
string(18) "Chelsea FC"
["id"]=>
string(3) "4"
between the second and third array element.
I have done it like this
$this->clubs = array_slice($this->clubs, 0, 2, true) +
['name' => 'Chelsea FC', 'id' => 4] +
array_slice($this->clubs, 2, sizeof($this->clubs), true);
However, this returns the following array
array(27) {
[0]=>
array(2) {
["name"]=>
string(16) "Arsenal FC"
["id"]=>
string(2) "1"
}
[1]=>
array(2) {
["name"]=>
string(8) "Liverpool FC"
["id"]=>
string(3) "2"
}
["name"]=>
string(20) "Chelsea FC"
["id"]=>
int(4)
[2]=>
array(2) {
["name"]=>
string(18) "Manchester United"
["id"]=>
string(3) "3"
}
As you can see, the array is added to the correct position, but not in the correct format (not multidimensional). What I would need to do now, is to add the array at the position and increase the count of all other indexes.
I tried to achieve it like this
if ($otherClubs->count() > 0) {
array_walk($this->clubs, function($item, $key) use($index, $otherClubs) {
if ($key > $index) {
$this->clubs[$key + $otherClubs->count()] = $item;
unset($this->otherClubs[$key]);
}
});
}
but this destroyed the array completely
array(36) {
[0]=>
array(2) {
["name"]=>
string(16) "Arsenal FC"
["id"]=>
string(2) "1"
}
[1]=>
array(2) {
["name"]=>
string(8) "Liverpool FC"
["id"]=>
string(3) "2"
}
["name"]=>
string(20) "Chelsea FC"
["id"]=>
int(3)
[2]=>
array(1) {
["id"]=>
string(3) "4"
}
How can I achieve this?