I have an array in variable and I want to put some elements between elements of these array. How can I make these? Here is var_dump of my array:
Array
(
[0] => Array
(
[label] => HOME
[url] => Array
(
[0] => /site/home
)
)
[1] => Array
(
[label] => Contact
[url] => Array
(
[0] => /site/contact
)
)
[2] => Array
(
[label] => Contact2
[url] => Array
(
[0] => /site/contact
)
)
)
Here is a php:
$itemsStatic = [
['label' => 'HOME', 'url' => ['/site/home']],
['label' => 'Contact', 'url' => ['/site/contact']],
['label' => 'Contact2', 'url' => ['/site/contact']],
];
Then I would like to put some elements between Contact and Contact2 and I call a function that must return elements:
$itemsStatic = [
['label' => 'HOME', 'url' => ['/site/home']],
['label' => 'Contact', 'url' => ['/site/contact']],
getElements(),
['label' => 'Contact2', 'url' => ['/site/contact']],
];
Here is a function:
function getElements()
{
$ret = [];
for ($i = 0; $i<2; $i++){
array_push( $ret, ['label' => 'HOME'.$i, 'url' => ['/site/home']]);
}
return $ret;
}
I getting these array
Array
(
[0] => Array
(
[label] => HOME
[url] => Array
(
[0] => /site/home
)
)
[1] => Array
(
[label] => Contact
[url] => Array
(
[0] => /site/contact
)
)
[2] => Array
(
[0] => Array
(
[label] => HOME0
[url] => Array
(
[0] => /site/home
)
)
[1] => Array
(
[label] => HOME1
[url] => Array
(
[0] => /site/home
)
)
)
[3] => Array
(
[label] => Contact2
[url] => Array
(
[0] => /site/contact
)
)
)
But I need to get array like these:
Array
(
[0] => Array
(
[label] => HOME
[url] => Array
(
[0] => /site/home
)
)
[1] => Array
(
[label] => Contact
[url] => Array
(
[0] => /site/contact
)
)
[2] => Array
(
[label] => HOME0
[url] => Array
(
[0] => /site/home
)
)
[3] => Array
(
[label] => HOME1
[url] => Array
(
[0] => /site/home
)
)
[4] => Array
(
[label] => Contact2
[url] => Array
(
[0] => /site/contact
)
)
)
Highly important for me to call a function like I describe above. Can anybody help me with these?
Use array_splice
to insert data into array at certain position:
array_splice($itemsStatic, 2, 0, getElements());