xxxxxxxxxx
$data['one'] = 1;
$data += [ "two" => 2 ];
$data += [ "three" => 3 ];
$data += [ "four" => 4 ];
//You can also, of course, append more than one element at once...
$data['one'] = 1;
$data += [ "two" => 2, "three" => 3 ];
$data += [ "four" => 4 ];
xxxxxxxxxx
$fruits = ["apple", "banana"];
// array_push() function inserts one or more elements to the end of an array
array_push($fruits, "orange");
// If you use array_push() to add one element to the array, it's better to use
// $fruits[] = because in that way there is no overhead of calling a function.
$fruits[] = "orange";
// output: Array ( [0] => apple [1] => banana [2] => orange )
xxxxxxxxxx
<?php
// Insert "blue" and "yellow" to the end of an array:
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
xxxxxxxxxx
<?php
$db_user = array("user_id", "user_name", "email");
array_push($db_user, "contact");
print_r($db_user);
?>
Output:
Array
(
[0] => user_id
[1] => user_name
[2] => email
[3] => contact
)
xxxxxxxxxx
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
xxxxxxxxxx
<?php
$z = ['me','you', 'he'];
array_push($z, 'she', 'it');
print_r($z);
?>
xxxxxxxxxx
<?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
)
*/
?>
xxxxxxxxxx
<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
?>
xxxxxxxxxx
$a = array('a','b','c');
$b = array('c','d','e');
array_push($a, $b);
print_r($a);
/*
notice this is different than array merge as it does not merge
values that the same
Array
(
[0] => a
[1] => b
[2] => c
[3] => c
[4] => d
[5] => e
)
*/
xxxxxxxxxx
$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]