当前位置:网站首页>How does PHP add an array element to an array
How does PHP add an array element to an array
2022-04-21 21:48:00 【Billion speed cloud】
php How to add an array element to an array
This article mainly explains “php How to add an array element to an array ”, The explanation in the text is simple and clear , Easy to learn and understand , Next, please follow Xiaobian's ideas and go deeper slowly , Study and learn together “php How to add an array element to an array ” Well !
The way to add elements :1、 Use “array_unshift( Array , Array elements )” sentence , Add an element at the beginning of the array ;2、 Use “array_push( Array , Array elements )” sentence , Add elements at the end of the array ;3、 use “array_pad( Array , The length of the array +1, Elements )” sentence .

The operating environment of this tutorial :windows7 System 、PHP7.1 edition 、DELL G3 The computer
php A method of adding an array element to an array
1、array_unshift() Function to insert a new element into the array
array_unshift($array,$value1,$value2...) The function can insert one or more new elements at the beginning of the array ( Key value ).
Let's take a concrete look through the following examples :
<?php$arr=array(10,12,20);array_unshift($arr,8);var_dump($arr);?>

array_unshift() The function does not maintain the original numerical index relationship , All key names will be deleted , And reassign it , From 0 Start counting again ; But all string key names remain the same .
<?phpheader("Content-type:text/html;charset=utf-8");$arr=array("a"=>"red","b"=>"green",3=>"pink");echo " The original array :";var_dump($arr);array_unshift($arr,"blue");echo " Insert a new element at the beginning :";var_dump($arr);?>
Output results :

2、array_push() Function to insert a new element into the array
array_push($array,$value1,$value2...) The function can insert one or more elements at the end of the array ( Key value ).
Let's take a concrete look through the following examples :
<?php$arr=array(10,12,20);array_push($arr,8);var_dump($arr);?>

array_push() Functions and array_unshift() Different functions , It does not reset the numeric key name , Instead, count on the basis of the original numeric key name .
<?phpheader("Content-type:text/html;charset=utf-8");$arr=array("a"=>"red","b"=>"green",3=>"pink");array_push($arr,8,"9",3.14);var_dump($arr);?>
Output results :

3、array_pad() Function to insert a new element into the array
array_pad($array,$size,$value) Function can take a key value $value Insert into array $array in , To fill the array to the specified length $size.($size Parameters can be understood as the final number of elements in the array , That is, the length of the array after the insert operation ).
Only need to $size Parameter set to Original array length +1 You can insert a new element
<?php$arr=array(10,12,20);$result =array_pad($arr,4,1);var_dump($result);?>

As can be seen from the above example ,array_pad() The function can insert elements at the end of the array , Actually array_pad() The function can also insert elements at the beginning of the array ; And the key is $size Parameters .
$size There are three values for parameters :
Is a positive number , Insert an element at the end of the array ;
It's a negative number , Insert an element at the beginning of the array ;
If its absolute value is less than or equal to
$arrayLength of array , The insert operation is not performed .
<?php$arr=array(10,12,20);$result =array_pad($arr,-5,1);var_dump($result);$result =array_pad($arr,3,1);var_dump($result);$result =array_pad($arr,2,1);var_dump($result);?>
The output is :

Parameters $value The value of can also be an array , Insert an entire array , Then the original array will become a two-dimensional array .
<?phpheader("Content-type:text/html;charset=utf-8");$arr=array(10,12,20);$result =array_pad($arr,-5,array(" Zhang San ",25," male "));var_dump($result);?>
The output is :

Thank you for reading , That's all “php How to add an array element to an array ” Content. , After learning this article , I'm sure you're right php I have a deeper understanding of how to add an array element to the array , The specific use needs to be verified by practice . This is billion speed cloud , Xiaobian will push you articles with more relevant knowledge points , Welcome to your attention !
版权声明
本文为[Billion speed cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212147080877.html
边栏推荐
- JS to realize automatic scrolling of announcements
- MySQL多表查询小练习
- Makefile file configure executable file and cflags parameter
- Hard core strength, recognized by many parties | cloud expansion technology, as the core manufacturer of RPA, was selected into the 2022 China RPA procurement guide
- 联想公布ESG新进展:承诺2025年全线计算机产品100%含有再生塑料
- 微服务,中台,RPA和低代码火热背后的一些冷思考
- Successful cases of project management system development
- Add subtitles to PR video
- 2022 crane driver (limited to bridge crane) work license question bank and online simulation examination
- 【迷人的爪哇】——数据类型和变量
猜你喜欢

Ffmpeg serial 2 - separate video and audio

一加连发两款耳机产品:充电10分钟 听歌20小时

TCP/IP协议

es6如何求两个数组的交集

Workflow engine parameter setting and business engine

Push to origin/master was rejected:报错解决办法

微服务,中台,RPA和低代码火热背后的一些冷思考

【测试用例级别定义】

Unity3d C uses the offset of material map to realize the function of unlimited moving background effect of 2D game single background image (including source code)

What should you do to benefit from digital transformation?
随机推荐
2022 safety officer-a certificate examination exercises and online simulation examination
初识线程安全有这一篇就够了
Lenovo announced the new progress of ESG: it promised that 100% of all computer products would contain recycled plastics by 2025
基于Redis实现分布式锁的7种方案
UML integrated design example
OA form design case display
2022年高处安装、维护、拆除考试题库及模拟考试
How to set the South parameter when streaming from libvlc library
Live555 learning
还清90万安居贷才能拿到离职证明?腾讯回应:与实际情况不符
RVB2601之启动流程
GStreamer learning
Online CSV to yaml tool
3D printer cr-10s cr10s Pro ender-3 Ender 3pro Ender 5 how to modify the pulse value or transmission value E of the extrusion motor when replacing the BMG extruder
Use try-with-resources or close this “FileOutputStream“
Pay off 900000 housing loans to get the resignation certificate? Tencent response: inconsistent with the actual situation
PHP数组函数extract 使用详解
Jz19 regular expression matching
力扣解法汇总824-山羊拉丁文
helm安装jenkins(官方)