1

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?

1 Answer 1

4

Since the item you want to insert is an array, and array_splice takes an array of items, put your array in an array.

Also since you want to insert something before the last element you can use a negative number for the offset to count backward from the end. (The effect is the same with the data you've got, but matters on arrays of different sizes.)

array_splice(
    $tpl['breadcrumbs'],
    -1,
    0,
    [[ 'title' => $category->category->name, 'text' => $category->category->name, 'url' => SITE_DOMAIN.ADMIN_PATH.'/items/' ]]
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.