对阵列内的数组执行array_push

时间:2016-07-01 02:56:29

标签: php

$mainstack = array(
            'applet' => "Flow"
        );

    $menustack = array(
        'applet' => "Menu",
        'repeat' => "2"
        );

    $dialstack = array(

        );

    $dial1 = array(
        'applet' => "Dial",
        'number' => "165465468",
        'whisper' => "Yes",
        );
    $dial2 = array(
        'applet' => "Dial",
        'number' => "654984",
        'whisper' => "No",
        );
    $dial3 = array(
        'applet' => "Dial",
        'number' => "398965165",
        'whisper' => "Yes",
        );

    array_push($mainstack, $menustack);
    array_push($dialstack, $dial1);
    array_push($dialstack, $dial2);
    array_push($dialstack, $dial3);
    array_push($menustack, $dialstack);

$dialstack放入$menustack后,我是否可以在menustack内插入$mainstack?通过将array_push($mainstack, $menustack);移动到最后一行,我想要的结果是可能的,但我真的在寻找一种方法,我可以将数组插入到现有的数组堆栈中。

2 个答案:

答案 0 :(得分:3)

你只需要知道$ mainstack里面$ menustack的索引

$arrayOne = [];
$arrayTwo = [];
$arrayThree = [];

// $arrayOne goes inside of $arrayTwo
array_push($arrayTwo, $arrayOne);

// $arrayThree goes inside of $arrayTwo[0] which is $arrayOne
array_push($arrayTwo[0], $arrayThree);

var_dump($arrayTwo);

答案 1 :(得分:1)

array_push($mainstack,array_merge($menustack,$dialstack));
print_r($mainstack);

首先使用array_merge合并两个数组,然后将其推送到$mainstack