如何拼接数组以在特定位置插入数组? [英] How to splice an array to insert array at specific position?

查看:222
本文介绍了如何拼接数组以在特定位置插入数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$custom = Array(
            Array(
                'name'  =>  $name1,
                'url'   =>  $url1
            ),
            Array(
                'name'  =>  $name_a,
                'url'   =>  $url_a
            )
        );

我正在尝试使用以下内容拼接数组:

I am attempting to splice the array with the following:

$bread_elem = array('name' => 'Golf', 'url' => $slug . $parent_slug);
array_splice($custom, 1, 0, $bread_elem);

我希望我的数组成为以下数组,将$sale_bread_elem的值插入数组中的位置1.我看不到我在做什么错.

I want my array to become the following, with the value of $sale_bread_elem inserted into position one within the array. I can't see what I am doing wrong.

$custom = Array(
            Array(
                'name'  =>  $name1,
                'url'   =>  $url1
            ),
            Array(
                'name'  =>  'Golf',
                'url'   =>  $slug . $parent_slug
            ),
            Array(
                'name'  =>  $name_a,
                'url'   =>  $url_a
            )
        );

推荐答案

array_splice ­ Docs 插入要插入的元素的 array .因此通话实际上应该是

array_splice­Docs takes an array of elements to insert. So the call should actually be

array_splice($custom, 1, 0, array($bread_elem));

这篇关于如何拼接数组以在特定位置插入数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆