Home >Backend Development >PHP Problem >How to change the original array value in php
PHP’s array is a very common data structure that is very convenient and flexible to use. In PHP, it is often necessary to modify the original array, and in some cases the value of the original array needs to be changed. This article will introduce how to change the original array value in PHP.
1. Array reference assignment
In PHP, if you use an assignment statement to assign a value to an array variable, then the new variable is a copy of the original array, that is, modify the new variable The value will not change the value of the original array. If you need to change the value of the original array, you need to use reference assignment. The reference assignment symbol "&" can be placed in front of a variable, which means that when assigning a variable to another variable, a reference to the original variable is used. Therefore, modifying the value of one variable simultaneously modifies the value of the other variable.
For example, the following code demonstrates the difference between reference assignment and traditional assignment:
// 传统的赋值语句 $array1 = array(1, 2, 3); $array2 = $array1; $array2[1] = 4; print_r($array1); // 输出 Array ( [0] => 1 [1] => 2 [2] => 3 ) print_r($array2); // 输出 Array ( [0] => 1 [1] => 4 [2] => 3 ) // 引用赋值语句 $array1 = array(1, 2, 3); $array2 = &$array1; $array2[1] = 4; print_r($array1); // 输出 Array ( [0] => 1 [1] => 4 [2] => 3 ) print_r($array2); // 输出 Array ( [0] => 1 [1] => 4 [2] => 3 )
It can be seen that in the code using reference assignment, modifying the value of $array2 will also modify the value of $array1. value. This is because $array2 is a reference to $array1 and they point to the same array.
2. Directly modify the value of the array element
In addition to using reference assignment, you can also directly change the value of the original array by modifying the value of the array element. You can use array subscripts to access and modify the values of array elements. For example, the following code demonstrates how to directly modify the value of an array element through the array subscript:
// 直接修改数组元素的值 $array = array(1, 2, 3); $array[1] = 4; print_r($array); // 输出 Array ( [0] => 1 [1] => 4 [2] => 3 )
As you can see, after modifying the value of $array[1] to 4, the value of $array is also changed. changed. This is because in PHP, an array is an ordered collection, and each element has a unique subscript to identify it. Therefore, accessing and modifying the values of array elements through array subscripts is a very common operation.
3. Use array functions to change the value of the original array
In PHP, you can also use a variety of array functions to change the value of the original array. These functions include adding and removing elements, sorting and filtering arrays, etc. Here are some commonly used array functions:
$array = array(1, 2, 3); array_push($array, 4, 5); print_r($array); // 输出 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
$array = array(1, 2, 3); array_pop($array); print_r($array); // 输出 Array ( [0] => 1 [1] => 2 )
$array = array(1, 2, 3); array_shift($array); print_r($array); // 输出 Array ( [0] => 2 [1] => 3 )
$array = array(1, 2, 3); array_unshift($array, 0); print_r($array); // 输出 Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
$array = array(1, 2, 3, 4, 5); array_splice($array, 1, 2, array(6, 7)); print_r($array); // 输出 Array ( [0] => 1 [1] => 6 [2] => 7 [3] => 4 [4] => 5 )
$array = array(3, 1, 4, 1, 5, 9); sort($array); print_r($array); // 输出 Array ( [0] => 1 [1] => 1 [2] => 3 [3] => 4 [4] => 5 [5] => 9 )
7.rsort: Sort the array in descending order.
$array = array(3, 1, 4, 1, 5, 9); rsort($array); print_r($array); // 输出 Array ( [0] => 9 [1] => 5 [2] => 4 [3] => 3 [4] => 1 [5] => 1 )
$array = array(1, 2, 3, 4, 5); $array = array_filter($array, function($value) { return $value % 2 == 0; }); print_r($array); // 输出 Array ( [1] => 2 [3] => 4 )
As can be seen from the above example, using these array functions can easily change the value of the original array, and improve the readability and maintainability of the code.
Conclusion
There are many ways to change the value of the original array in PHP. You can use reference assignment, directly modify the value of the array element, and use array functions. The choice of these methods depends on actual needs and convenience. I hope this article can provide some help to PHP developers and make them more proficient in using arrays and processing data.
The above is the detailed content of How to change the original array value in php. For more information, please follow other related articles on the PHP Chinese website!