I have the following array structure:
$tpl = [
'breadcrumbs' => [
[ 'title' => 'Item Database', 'text' => 'Item Database', 'url' => SITE_DOMAIN.ADMIN_PATH.'/items/' ],
[ 'title' => $category->name, 'text' => $category->name, 'active' => true ]
]
];
I am attempting to insert an element before the last element, so thought I could use array_splice as follows:
if( !is_null($category->category) )
{
array_splice(
$tpl['breadcrumbs'],
1,
0,
[ 'title' => $category->category->name, 'text' => $category->category->name, 'url' => SITE_DOMAIN.ADMIN_PATH.'/items/' ]
);
}
However, this seems to flatten the item I am trying to insert (as per the expected behaviour) and produces the following output:
Array
(
[0] => Array
(
[title] => Item Database
[text] => Item Database
[url] => https://local/qmdepot/admin/items/
)
[1] => Medical Department
[2] => Medical Department
[3] => https://local/qmdepot/admin/items/
[4] => Array
(
[title] => Class 1
[text] => Class 1
[active] => 1
)
)
While the expected output should be:
Array
(
[0] => Array
(
[title] => Item Database
[text] => Item Database
[url] => https://local/qmdepot/admin/items/
)
[1] => Array
(
[title] => Medical Department
[text] => Medical Department
[url] => https://local/qmdepot/admin/items/
)
[2] => Array
(
[title] => Class 1
[text] => Class 1
[active] => 1
)
)
I am able to achieve this using the following code, but it feels a little hacky to me:
# Set up the breadcrumbs:
if( !is_null($category->category) )
{
$tpl['breadcrumbs'][2] = $tpl['breadcrumbs'][1];
$tpl['breadcrumbs'][1] = [ 'title' => $category->category->name, 'text' => $category->category->name, 'url' => SITE_DOMAIN.ADMIN_PATH.'/items/' ];
}
Is there any way to insert an array item into a multi-dimensional array at a specified index without writing a custom function or using the hack above?