// for php 5.4+
$data += [$key => $value];
// for php 5.4-
$data += array($key => $value);
<?php
// Insert "blue" and "yellow" to the end of an array:
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
$a1=['aa'=>'123' , 'bb'=>'454'];
$a1 = array_merge( $a1 , ['a'=>1,'b'=>2] ) ;
$a = array('foo' => 'bar'); // when you create
$a['Title'] = 'blah'; // later
$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>";
array_push ( array &$array [, mixed $... ] ) : int
or
$array[] = $var;
// array_push ( array &$array [, mixed $... ] ) : int
// array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:
<?php
$array[] = $var;
?>
// repeated for each passed value.
// Note: If you use array_push() to add one element to the array, it's better to use $array[] = because in that way there is no overhead of calling a function.
If you're going to use array_push() to insert a "$key" => "$value" pair into an array, it can be done using the following:
$data[$key] = $value;
It is not necessary to use array_push.
$data[$category][] = $item;
PHP function array_push(array &$array, ...$values) int
------------------------------------------------------
Push elements onto the end of array. Since 7.3.0 this function can be called with only one parameter.
For earlier versions at least two parameters are required.
Parameters:
array--$array--The input array.
mixed--...$values--[optional] The pushed variables.
Returns: the number of elements in the array.
$arr = array(
"name" => "jonh",
"Mob" => "588555",
"Email" => "jonh143@gmail.com"
);
$arr['Country'] = "United State";