array_push
PHP array_push()函數 (PHP array_push() function)
array_push() function is used to insert/push one or more than one element to the array.
array_push()函數用于將一個或多個元素插入/推入數組。
Syntax:
句法:
array_push(array, elemement1, [element2],..);
Here,
這里,
array is the source array in which we have to push the elements.
array是我們必須在其中推送元素的源數組。
element is the value to be added, we can add more than one elements too.
element是要添加的值,我們也可以添加多個元素。
Examples:
例子:
Input:
//initial array is
$arr = array(100, 200, 300);
//pushing elements
array_push($arr, 10);
array_push($arr, 400, 500);
Output:
Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 10
[4] => 400
[5] => 500
)
PHP code 1: Inserting elements in an indexed array
PHP代碼1:在索引數組中插入元素
<?php
$arr = array(100, 200, 300);
array_push($arr, 10);
array_push($arr, 400, 500);
print("array after inserting elements...\n");
print_r($arr);
?>
Output
輸出量
random key: age
array of random keys...
Array
(
[0] => age
[1] => city
)
PHP code 2: Inserting elements in an associative array
PHP代碼2:在關聯數組中插入元素
<?php
$arr = array("name" => "Amit", "age" => 21);
array_push($arr, "Gwalior");
array_push($arr, "Male", "RGTU University");
print("array after inserting elements...\n");
print_r($arr);
?>
Output
輸出量
array after inserting elements...
Array
(
[name] => Amit
[age] => 21
[0] => Gwalior
[1] => Male
[2] => RGTU University
)
See the output – There are two keys name and age and we added three more elements "Gwalior", "Male" and "RGTU University", these three elements added with the keys 0, 1 and 2.
看到輸出-有兩個關鍵的姓名和年齡 ,我們增加了三個要素“瓜廖爾”,“ 男性”和“RGTU大學”,與鍵0,1和2添加了這三個要素。
翻譯自: https://www.includehelp.com/php/array_push-function-with-example.aspx
array_push