【问题标题】:How to insert new array to a specific position in multi dimensional array in php如何将新数组插入到php中多维数组中的特定位置
【发布时间】:2021-05-01 10:21:57
【问题描述】:

如何将新数组插入到多维数组的特定位置下面是代码,如何使用php完成

$aExistingArray = Array
    (
[pages] => Array
    (
        [0] => Array
            (
                [name] => page1
                [elements] => Array
                    (
                        [0] => Array
                            (
                                [type] => text
                                [name] => question2
                                [title] => PSTN
                            )

                        [1] => Array
                            (
                                [type] => radiogroup
                                [name] => question3
                                [title] => Are you satisfied to our services
                                [choices] => Array
                                    (
                                        [0] => Array
                                            (
                                                [value] => item1
                                                [text] => yes
                                            )

                                        [1] => Array
                                            (
                                                [value] => item2
                                                [text] => no
                                            )

                                    )

                            )

                    )

            )

    ))

下面是我要插入到 elements 位置 0 位置的新数组,它将如何使用数组技术完成

$aToBeInserted = Array
    (
[0] => Array
    (
        [type] => text
        [name] => name
        [title] => name
        )

[1] => Array
    (
        [type] => text
        [name] => question1
        [title] => Test
    )

)

【问题讨论】:

  • 您要插入什么数组?您只在上面的帖子中提供了原始数组。
  • 链接副本为您提供了机制。您真正需要提供的只是新元素和您希望插入的确切子数组。
  • @El_Vanja 新元素已发布请查收
  • @BlakeOttinger 问题已编辑,请检查

标签: php arrays


【解决方案1】:

解决方法如下,先找到数组的具体索引,合并两个数组,然后用原来的数组覆盖。

$elementPostition = $arr['pages'][0]['elements'];
$aMergeArray = array_merge($sTobeInsert,$elementPostition );
$arr['pages'][0]['elements'] = $aMergeArray;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多