Home > Article > Backend Development > How to change a value in an array in php
PHP is a commonly used server-side programming language that can easily handle various data types, especially arrays. In PHP, we often need to operate on arrays, and one common operation is to change one or more values in the array. Let's take a look at how to change a value in an array in PHP.
1. Change the value in the array through index
The most common array type in PHP is the index array, that is, the value in the array is numerically indexed. We can change it by changing the value of the index. the values in the array. For example:
$array = array('apple', 'banana', 'orange'); $array[1] = 'pear'; print_r($array);
The output result is:
Array ( [0] => apple [1] => pear [2] => orange )
We changed the original "banana" at index 1 to "pear".
2. Change the value in the associative array through the key name
Associative array is another common array type in PHP, which stores data in the form of key-value pairs. We can change the value in an associative array by key name. For example:
$array = array('name' => 'Tom', 'age' => 18, 'gender' => 'male'); $array['age'] = 19; print_r($array);
The output result is:
Array ( [name] => Tom [age] => 19 [gender] => male )
We changed the value of the original key name "age" from 18 to 19.
3. Change the value in the array through the array_splice() function
If you want to insert a new element in the middle of the array and change the value in the original array, we can use the array_splice() function. This function can insert a new element at a specified position and return the removed element, thereby changing a value in the array.
$array = array('apple', 'banana', 'orange'); array_splice($array, 1, 1, 'pear'); print_r($array);
The output result is:
Array ( [0] => apple [1] => pear [2] => orange )
In this example, we use the array_splice() function to remove "banana" with index 1 in the array and replace it with the new value "pear" it. Finally, we print out the modified array through the print_r() function.
4. Change the values in the array through the array_replace() function
If you want to replace the values in one array with the values in another array, we can use the array_replace() function. This function replaces the values in the first array with the corresponding values in the second array and returns the resulting array.
$array1 = array('apple', 'banana', 'orange'); $array2 = array(0 => 'pear', 2 => 'peach'); $result = array_replace($array1, $array2); print_r($result);
The output result is:
Array ( [0] => pear [1] => banana [2] => peach )
In this example, we replace the values with indexes 0 and 2 in the $array1 array with the values "pear" and "pear" in the $array2 array respectively. "peach", the final result array returned is the modified array. It should be noted that if there are duplicate key names in the array, the later values will overwrite the previous values.
Summary
There are many ways to change a value in an array in PHP. Common ones include changing the value in the array through index, changing the value in associative array through key name, and using array_splice. () function changes the value in the array, and replaces the value in the array through the array_replace() function. Using these methods can easily modify arrays in PHP, avoiding tedious manual operations.
The above is the detailed content of How to change a value in an array in php. For more information, please follow other related articles on the PHP Chinese website!