我怎样才能得到这样的数组?

时间:2019-04-12 09:17:21

标签: php arrays

我有一个变量数组,我想在这些数组的元素之间放置一些元素。我该怎么做? 这是我的数组的var_dump:

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
                )

        )

)

这是一个php:

$itemsStatic =  [

        ['label' => 'HOME', 'url' => ['/site/home']],
        ['label' => 'Contact', 'url' => ['/site/contact']],
        ['label' => 'Contact2', 'url' => ['/site/contact']],

    ];

然后我想在Contact和Contact2之间放置一些元素,然后调用一个必须返回元素的函数:

$itemsStatic =  [

        ['label' => 'HOME', 'url' => ['/site/home']],
        ['label' => 'Contact', 'url' => ['/site/contact']],
        getElements(),
        ['label' => 'Contact2', 'url' => ['/site/contact']],

    ];

这是一个功能:

function getElements()
{

    $ret = [];
    for ($i = 0; $i<2; $i++){
        array_push( $ret, ['label' => 'HOME'.$i, 'url' => ['/site/home']]);
    }

    return $ret;



}

我得到了这些数组

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
                )

        )

)

但是我需要获得像这样的数组:

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
                )

        )

)

对我来说,如上所述调用一个函数非常重要。 有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

使用array_splice将数据插入数组中的特定位置:

array_splice($itemsStatic, 2, 0, getElements());