Beldion Bryan Balanan Beldion Bryan Balanan - 5 months ago 8
PHP Question

doing an array_push to an array inside an array

$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);


Is there a way where I can insert
$dialstack
inside of
$menustack
after putting
menustack
inside of
$mainstack
? The outcome I want is possible by moving
array_push($mainstack, $menustack);
to the last line, but I am really looking for a way where I can just insert an array to an existing stack of arrays.

Answer

You just need to know the index of $menustack inside of $mainstack

$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);
Comments