In PHP, array is a commonly used data type. In actual development, it is often necessary to modify the elements in the array. This article will introduce how to use PHP to modify the array value.
1. Basic PHP array operations
In PHP, you can use the following two methods to define arrays:
// 方式一: $array1 = array('a', 'b', 'c'); // 方式二: $array2 = ['a', 'b', 'c'];
For an existing array, you can use the following methods to obtain it Its elements:
$array = array('a', 'b', 'c'); $element = $array[0]; // 获取数组中第一个元素 a
You can use subscripts to modify the elements in the array:
$array = array('a', 'b', 'c'); $array[0] = 'x'; // 修改数组中第一个元素为 x
2. Modify the array value
In addition to the above basic operations, in PHP Many functions are also provided to modify arrays. This article will introduce several commonly used functions.
array_replace function can be used to replace elements in an array. If there are the same key names in the array, the later elements will overwrite the previous elements. Sample code:
$array1 = array('a', 'b', 'c'); $array2 = array('A', 'B', 'C'); $newArray = array_replace($array1, $array2); print_r($newArray);
The output is as follows:
Array ( [0] => A [1] => B [2] => C )
array_fill function can be used to fill a new array. The sample code is as follows:
$newArray = array_fill(0, 3, 'x'); print_r($newArray);
The output result is as follows:
Array ( [0] => x [1] => x [2] => x )
array_pad function can be used to fill an array to a specified length. If the length is shorter than the original array, the empty positions are filled with the specified value. The sample code is as follows:
$array = array('a', 'b', 'c'); $newArray = array_pad($array, 5, 'x'); print_r($newArray);
The output is as follows:
Array ( [0] => a [1] => b [2] => c [3] => x [4] => x )
array_reverse function can be used to reverse the order of elements in an array. The sample code is as follows:
$array = array('a', 'b', 'c'); $newArray = array_reverse($array); print_r($newArray);
The output result is as follows:
Array ( [0] => c [1] => b [2] => a )
array_merge function can be used to merge multiple arrays into one array. If there are the same key names in the array, the later elements will overwrite the previous elements. The sample code is as follows:
$array1 = array('a', 'b', 'c'); $array2 = array('A', 'B', 'C'); $newArray = array_merge($array1, $array2); print_r($newArray);
The output result is as follows:
Array ( [0] => a [1] => b [2] => c [3] => A [4] => B [5] => C )
array_walk function can be used to apply user customization to each element in the array. defined function. The sample code is as follows:
$array = array('a', 'b', 'c'); function addPrefix(&$value, $key, $prefix) { $value = $prefix . $value; } array_walk($array, 'addPrefix', 'x_'); print_r($array);
The output result is as follows:
Array ( [0] => x_a [1] => x_b [2] => x_c )
3. Summary
This article introduces several commonly used PHP array functions, including array_replace, array_fill, array_pad, array_reverse , array_merge and array_walk. These functions can easily modify elements in the array, improving development efficiency. It should be noted that when modifying the array value, the order of the array subscripts should be kept in mind to avoid errors.
The above is the detailed content of php modify array value. For more information, please follow other related articles on the PHP Chinese website!