要在PHP数组中添加元素,可以使用以下两种方法:
array_push()
函数将一个或多个元素添加到数组的末尾。例如:$myArray = array("apple", "banana", "orange");
array_push($myArray, "grape", "mango");
print_r($myArray);
输出:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
[4] => mango
)
$myArray = array("apple", "banana", "orange");
$myArray[3] = "grape";
$myArray[4] = "mango";
print_r($myArray);
输出:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
[4] => mango
)
在这两种方法中,都可以一次添加一个或多个元素到数组中。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站