在 PHP 中,可以使用 array_push() 或 [] 运算符来将元素添加到数组的末尾。

使用 array_push() 函数:

$fruits = array("apple", "banana");
array_push($fruits, "orange", "pear");
print_r($fruits);

输出:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => pear
)

使用 [] 运算符:

$fruits = ["apple", "banana"];
$fruits[] = "orange";
$fruits[] = "pear";
print_r($fruits);

输出:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => pear
)

两种方法都可以将元素添加到数组的末尾。

php 数组怎么append

原文地址: https://www.cveoy.top/t/topic/g9rj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录