php中array_push()和array_pop()及array_shift() 函数用法示例详解

怪我咯
Release: 2023-03-08 06:20:01
Original
5145 people have browsed it

php数组函数array_push()和array_pop()以及array_shift()各自的用法?

php array_push函数是向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度。array_pop() 函数删除数组中的最后一个元素。array_shift() 函数删除数组中第

一个元素,并返回被删除元素的值。本篇文章将分别为大家来介绍这三个函数,希望对大家学习数组函数有所帮助。

array_push函数

array_push() 函数向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度。如果第一个参数不是数组,array_push() 将发出一条警告。

示例一

使用array_push() 函数向向数组尾部插入新的元素,代码如下

Copy after login

代码输出结果:

3.jpg


array_pop() 函数

array_pop() 函数删除数组中的最后一个元素。如果数组是空的,或者非数组,将返回 NULL。

示例二

使用array_pop() 函数删除数组中的最后一个元素,代码如下

Copy after login

代码输出结果:

4.jpg

array_shift() 函数

array_shift() 函数删除数组中第一个元素,并返回被删除元素的值。如果数组为空则返回 NULL。

示例三

array_shift() 函数删除数组中的第一个元素(red),并返回被删除元素的值,代码如下

"red","b"=>"green","c"=>"blue");
echo array_shift($a)."
"; print_r ($a); ?>
Copy after login

代码输出结果:

5.jpg

【相关文章推荐】

php数组函数array_push()的定义与用法

php array_pop()函数删除数组中的最后一个元素实例详解

详解php array_shift()函数:删除数组中第一个元素

The above is the detailed content of php中array_push()和array_pop()及array_shift() 函数用法示例详解. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!