xxxxxxxxxx
$array[$key] = $value;
// or
$array[] = $value;
// or
array_push($array, [ mixed $ ]);
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
$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
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
?>
xxxxxxxxxx
// The array_push() function inserts one or more elements to the end of an array.
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
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]
xxxxxxxxxx
<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
?>
xxxxxxxxxx
$cart = array();
$cart[] = 13;
$cart[] = 14;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart[] = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";