简体   繁体   中英

How to insert new array to a specific position in multi dimensional array in php

How to insert new array to a specific position in multi dimensional array below is the code, how it will be done using 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
                                            )

                                    )

                            )

                    )

            )

    ))

Below is the new array which I want to insert to elements position at 0 position, how it will be done using array technique

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

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

)

Below is the solution, first find the specific index of array, merge the both arrays and then override with original array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM