PHP追加数组
时间: 2023-09-03 10:05:54 浏览: 93
PHP中的数组
要向 PHP 数组追加元素,可以使用 `array_push()` 函数或者直接使用数组的 `[]` 运算符。下面是使用这两种方法向数组追加元素的示例代码:
使用 `array_push()` 函数:
```php
$myArray = array("apple", "banana", "orange");
array_push($myArray, "grape", "kiwi");
print_r($myArray);
```
输出:
```
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
[4] => kiwi
)
```
使用 `[]` 运算符:
```php
$myArray = array("apple", "banana", "orange");
$myArray[] = "grape";
$myArray[] = "kiwi";
print_r($myArray);
```
输出:
```
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
[4] => kiwi
)
```
阅读全文