在陣列上按值

將元素推送到陣列有兩種方法:array_push$array[] =

array_push 這樣使用:

$array = [1,2,3];
$newArraySize = array_push($array, 5, 6); // The method returns the new size of the array
print_r($array); // Array is passed by reference, therefore the original array is modified to contain the new elements

此程式碼將列印:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
    [4] => 6
)

$array[] = 使用如下:

$array = [1,2,3];
$array[] = 5;
$array[] = 6;
print_r($array);

此程式碼將列印:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
    [4] => 6
)