在PHP中,可以使用数组的array_push()
函数来插入新的元素。该函数将一个或多个值添加到数组的末尾,并返回新数组的长度。
以下是使用array_push()
函数插入数组的示例:
$fruits = array("apple", "banana", "orange");
// 插入一个元素
array_push($fruits, "grape");
// 插入多个元素
array_push($fruits, "watermelon", "kiwi");
print_r($fruits);
输出结果:
Array
(
[0] => apple
[1] => banana
[2] => orange
[3] => grape
[4] => watermelon
[5] => kiwi
)
在上面的示例中,我们首先创建了一个包含三个元素的数组$fruits
。然后,使用array_push()
函数将一个元素"grape"
插入到数组的末尾,再将两个元素"watermelon"
和"kiwi"
插入到数组的末尾。最后,使用print_r()
函数打印输出数组的内容。
除了array_push()
函数,还可以使用数组的[]
操作符来插入新的元素。例如:
$fruits[] = "grape";
$fruits[] = "watermelon";
$fruits[] = "kiwi";
这样也可以将元素插入到数组的末尾。
上一篇:php headers_sent() 函数检查 HTTP 报头是否发送/已发送到何处。
下一篇:php如何传数组
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站