How to change the original array value in php

PHPz
Release: 2023-04-19 10:36:20
Original
491 people have browsed it

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 )
Copy after login

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 )
Copy after login

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:

  1. array_push: Add one or more elements to the end of the array.
$array = array(1, 2, 3);
array_push($array, 4, 5);
print_r($array); // 输出 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Copy after login
  1. array_pop: Removes an element from the end of the array.
$array = array(1, 2, 3);
array_pop($array);
print_r($array); // 输出 Array ( [0] => 1 [1] => 2 )
Copy after login
  1. array_shift: Remove an element from the beginning of the array.
$array = array(1, 2, 3);
array_shift($array);
print_r($array); // 输出 Array ( [0] => 2 [1] => 3 )
Copy after login
  1. array_unshift: Adds one or more elements to the beginning of the array.
$array = array(1, 2, 3);
array_unshift($array, 0);
print_r($array); // 输出 Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
Copy after login
  1. array_splice: Removes a specified number of elements from an array and replaces them with new elements.
$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 )
Copy after login
  1. sort: Sort the array in ascending order.
$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 )
Copy after login

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 )
Copy after login
  1. array_filter: Filter elements in the array that do not meet the conditions.
$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 )
Copy after login

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!

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 admin@php.cn
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!