add array to array php
snippet in php

php append to array

user7885

$myArr = [1, 2, 3, 4];

array_push($myArr, 5, 8);
print_r($myArr); // [1, 2, 3, 4, 5, 8]

$myArr[] = -1;
print_r($myArr); // [1, 2, 3, 4, 5, 8, -1]

array push php

user4946


<?php

$a=array("red","green");

array_push($a,"blue","yellow");

print_r($a);
?>
 

php array add

user6040

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

php append element to array

user7836

array_push($cart, 13);

add array to array php

user3737

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
/*
Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)
*/
?>

add object in array php

user9885

$myArray = array("name" => "my name");
echo json_encode($myArray);