Home Backend Development PHP Problem How to change the original array value in php

How to change the original array value in php

Apr 19, 2023 am 10:04 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

This article explores efficient PHP array deduplication. It compares built-in functions like array_unique() with custom hashmap approaches, highlighting performance trade-offs based on array size and data type. The optimal method depends on profili

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

This article analyzes PHP array deduplication, highlighting performance bottlenecks of naive approaches (O(n²)). It explores efficient alternatives using array_unique() with custom functions, SplObjectStorage, and HashSet implementations, achieving

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

This article explores PHP array deduplication using key uniqueness. While not a direct duplicate removal method, leveraging key uniqueness allows for creating a new array with unique values by mapping values to keys, overwriting duplicates. This ap

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

This article explores optimizing PHP array deduplication for large datasets. It examines techniques like array_unique(), array_flip(), SplObjectStorage, and pre-sorting, comparing their efficiency. For massive datasets, it suggests chunking, datab

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea

See all articles