php array_push()函数用法
时间: 2023-09-19 20:09:47 浏览: 57
array_push()函数用于向数组末尾添加一个或多个元素。
语法:
array_push(array, value1, value2, ...)
参数:
array:必需。要添加元素的数组。
value1, value2, ...:可选。要添加到数组末尾的值。
返回值:
成功时返回新数组的元素个数。
例子:
向数组$fruits中添加元素"orange"和"banana"。
$fruits = array("apple", "pear");
array_push($fruits, "orange", "banana");
print_r($fruits);
输出:
Array
(
[0] => apple
[1] => pear
[2] => orange
[3] => banana
)
相关问题
在PHP中,array_pop() array_push() array_unshift() array_shift() array_unique() array_slice() array_splice()这些函数的作用与简单用法举例
以下是在PHP中这些函数的作用与简单用法举例:
1. array_pop():从数组末尾删除并返回最后一个元素。
```php
$arr = array('one', 'two', 'three');
$lastElement = array_pop($arr);
echo $lastElement; // 输出:three
```
2. array_push():将一个或多个元素添加到数组末尾。
```php
$arr = array('one', 'two');
array_push($arr, 'three', 'four');
print_r($arr); // 输出:Array ( [0] => one [1] => two [2] => three [3] => four )
```
3. array_unshift():将一个或多个元素添加到数组开头。
```php
$arr = array('two', 'three');
array_unshift($arr, 'one');
print_r($arr); // 输出:Array ( [0] => one [1] => two [2] => three )
```
4. array_shift():从数组开头删除并返回第一个元素。
```php
$arr = array('one', 'two', 'three');
$firstElement = array_shift($arr);
echo $firstElement; // 输出:one
```
5. array_unique():移除数组中重复的值,并返回新的数组。
```php
$arr = array('one', 'two', 'two', 'three');
$uniqueArr = array_unique($arr);
print_r($uniqueArr); // 输出:Array ( [0] => one [1] => two [3] => three )
```
6. array_slice():从数组中取出一段元素。
```php
$arr = array('one', 'two', 'three', 'four', 'five');
$slicedArr = array_slice($arr, 1, 3);
print_r($slicedArr); // 输出:Array ( [0] => two [1] => three [2] => four )
```
7. array_splice():从数组中移除或替换一段元素,并将被移除的元素替换为新的元素。
```php
$arr = array('one', 'two', 'three', 'four', 'five');
$removedArr = array_splice($arr, 1, 2, array('new'));
print_r($arr); // 输出:Array ( [0] => one [1] => new [2] => four [3] => five )
print_r($removedArr); // 输出:Array ( [0] => two [1] => three )
```
array_push
array_push() 是 PHP 中的一个内置函数,用于向数组末尾添加一个或多个元素。语法如下:
array_push(array $array, mixed $value1 [, mixed $... ]): int
其中,$array 是要操作的数组,$value1 到 $valueN 是要添加到数组末尾的元素。
使用方法示例:
$fruits = array("apple", "banana");
array_push($fruits, "orange", "grape");
上面的代码会向 $fruits 数组末尾添加 "orange" 和 "grape" 两个元素。
注意:array_push() 函数会修改原数组,并返回修改后数组的元素个数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)