Home > Article > Backend Development > How to transfer data in php array
In PHP, array is a very common and practical data type. Sometimes we need to transfer data from one array to another array. In this case, we need to use the array transfer function in PHP.
PHP provides 3 different array transfer functions, they are: array_push(), array_unshift() and array_shift(). Below we will introduce the usage and precautions of these three functions respectively.
1. array_push()
The array_push() function is used to add one or more elements to the end of the array and return the number of elements of the new array. The syntax of this function is as follows:
array_push(array $array, mixed $value1 [, mixed $... ])
For example:
$arr1 = array("apple", "banana", "orange"); // 原始数组 array_push($arr1, "peach", "grape"); // 向数组中添加两个元素 print_r($arr1); // 输出结果:Array ( [0] => apple [1] => banana [2] => orange [3] => peach [4] => grape )
In the above example, we first declare an original array $arr1, and then use the array_push() function to push two new elements "peach ” and “grape” are added to the end of the array.
This example is very simple, but please note that if you want to add multiple elements, you can reuse the array_push() function, or use the ellipsis "..." when calling the function.
2. array_unshift()
The array_unshift() function is used to add one or more elements to the beginning of the array and return the number of elements of the new array. The syntax and usage of this function are similar to the array_push() function, the only difference lies in the position where the elements are added.
For example:
$arr2 = array("red", "green", "blue"); // 原始数组 array_unshift($arr2, "yellow", "purple"); // 向数组中添加两个元素 print_r($arr2); // 输出结果:Array ( [0] => yellow [1] => purple [2] => red [3] => green [4] => blue )
In the above example, we use the array_unshift() function to add two new elements "yellow" and "purple" to the beginning of the array. Likewise, if you want to add multiple elements, you can reuse the array_unshift() function, or use the ellipsis "..." when calling the function.
3. array_shift()
The array_shift() function is used to remove the first element in the array and return the value of the element. The syntax of this function is as follows:
array_shift(array &$array)
For example:
$arr3 = array("a", "b", "c"); // 原始数组 $elem1 = array_shift($arr3); // 移除数组中的第一个元素,并返回该元素的值 print_r($arr3); // 输出结果:Array ( [0] => b [1] => c ) echo $elem1; // 输出结果:a
In the above example, we use the array_shift() function to remove the first element "a" in the array and change its value Saved in variable $elem1. It should be noted that if the array is empty, this function will return NULL.
One thing to note here is that if you remove an element from an array, the index position of the element in the array will be deleted, and the index positions of subsequent elements will also be decremented.
In PHP development, array transfer is a very common operation. This article introduces three different array transfer functions in PHP: array_push(), array_unshift() and array_shift(). Their usage and syntax are relatively simple, but they need to be used according to actual needs. Mastering these functions will help you better handle array-related operations in PHP.
The above is the detailed content of How to transfer data in php array. For more information, please follow other related articles on the PHP Chinese website!