$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 )
$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
)
*/
// 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.
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.