Home >Backend Development >PHP Tutorial >Tips for learning PHP arrays and using functions to insert elements (1)
In the previous article, we introduced two alternative methods of creating arrays - filling method to create new arrays. If you are interested, you can click on the link to read → "PHP Array Learning: How to Create a Filling Method Array》. Let me introduce to you how to insert new elements into PHP arrays. Let’s take a look.
There are multiple functions built into PHP that can add new elements to the array, such as array_unshift(), array_push(), array_pad(), array_splice(), etc. This article will first introduce you to two of the functions, array_unshift() and array_push(), and I will introduce you to the next two functions in the next article.
The following is a code example to introduce in detail how the array_unshift() and array_push() functions insert new elements.
1. The array_unshift() function inserts new elements into the array
array_unshift($array,$value1,$value2...)
The function can insert one or more new elements (key values) at the beginning of the array.
Let’s take a closer look at the following example:
<?php $arr=array(10,12,20); array_unshift($arr,8,"9"); var_dump($arr); ?>
array_unshift($arr,8,9)
It can be seen that two characters are inserted at the beginning of the $arr array new elements: the value "8
" and the string "9
", so the output result is:
array_unshift() The function will not maintain the original numerical index relationship, but will delete all numerical key names and reassign them, that is, count again from 0; but all string key names will remain unchanged.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array("a"=>"red","b"=>"green",3=>"pink"); echo "原来的数组:"; var_dump($arr); array_unshift($arr,"blue"); echo "在开头插入一个新元素后:"; var_dump($arr); ?>
Output result:
2. The array_push() function inserts new elements into the array
array_push($array,$value1,$value2...)
The function can insert one or more elements (key values) at the end of the array.
Let’s take a closer look at the following example:
<?php $arr=array(10,12,20); array_push($arr,8,"9",3.14); var_dump($arr); ?>
array_push($arr,8,"9",3.14)
It can be seen that in the $arr array Insert 3 elements at the end: integer "8
", string "9
" and floating point number "3.14
", so the output result is:
The array_push() function is different from the array_unshift() function. It does not reset the numerical key name, but counts based on the original numerical key name.
<?php header("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 result:
Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial
Finally, I would like to recommend a free video tutorial on PHP arrays: PHP function array array function video explanation, come and learn!
The above is the detailed content of Tips for learning PHP arrays and using functions to insert elements (1). For more information, please follow other related articles on the PHP Chinese website!