In PHP, two-dimensional array is a common data type. It consists of multiple one-dimensional arrays, each one-dimensional array represents a set of data. Two-dimensional arrays can be used to store tabular data, multi-dimensional categorical data and other scenarios. In development, we often need to perform operations such as adding, deleting, modifying, and checking two-dimensional arrays. This article will share how to add value to two-dimensional arrays in PHP.
1. Basic knowledge of two-dimensional arrays
In PHP, we can create an empty two-dimensional array through array() or [], for example:
$two_dim_array = array( array(1, 2, 3), array('a', 'b', 'c'), array('x', 'y', 'z') );
The above code creates a 3x3 two-dimensional array, where each one-dimensional array contains three elements. We can access elements in a two-dimensional array through indexes, for example:
echo $two_dim_array[0][1]; // 输出 2 echo $two_dim_array[1][2]; // 输出 c echo $two_dim_array[2][0]; // 输出 x
2. Add new elements to a two-dimensional array
There are many ways to add new elements to a two-dimensional array in PHP , the common methods are as follows:
array_push() function can add one or more elements to the end of the array. In a two-dimensional array, we can add a new one-dimensional array as an element to the end, for example:
$new_arr = array('p', 'q', 'r'); array_push($two_dim_array, $new_arr);
After executing the above code, the $two_dim_array array will add a new one-dimensional array containing three elements 'p', 'q' and 'r'.
Direct assignment is a simple and fast method to add a new element at a specified position. For example, if we want to add a new element '4' to the first one-dimensional array, we can use the following code:
$two_dim_array[0][] = 4;
What this method does is add a new element to the end of the $two_dim_array[0] array '4'.
Use double loop to add new elements to any position. For example, if we want to insert a new element 'd' at the third position of the second one-dimensional array, we can use the following code:
$new_elem = 'd'; $index = 2; for ($i = 0; $i < count($two_dim_array); $i++) { for ($j = 0; $j < count($two_dim_array[$i]); $j++) { if ($i == 1 && $j == $index) { array_splice($two_dim_array[$i], $j, 0, $new_elem); } } }
In the above code, a double loop is used to traverse the entire two-dimensional array , when looping to the third position of the second one-dimensional array, use the array_splice() function to insert a new element 'd' at that position.
3. Modify element values to the two-dimensional array
The element values in the two-dimensional array can be modified through assignment operations. For example, if we want to modify the second element 'y' of the third one-dimensional array to 'w', we can use the following code:
$two_dim_array[2][1] = 'w';
After executing the above code, 'y' in the $two_dim_array array ' will be replaced with 'w'.
4. Delete elements from a two-dimensional array
The method of deleting elements from a two-dimensional array in PHP is similar to a one-dimensional array. Common methods include the following:
You can use the unset() function to delete the specified element in the array. For example, if we want to delete the second element '2' of the first one-dimensional array, we can use the following code:
unset($two_dim_array[0][1]);
Use the array_splice() function to remove one or more elements from an array and replace them with a specified value. For example, if we want to delete the third element 'c' of the second one-dimensional array, we can use the following code:
array_splice($two_dim_array[1], 2, 1);
You can use the array_pop() function to delete elements at the end of the array, and use the array_shift() function to delete elements at the beginning of the array. However, these two functions can only delete elements in one-dimensional arrays.
5. Summary
In PHP development, two-dimensional array is a very common data type. We need to master the methods of adding, deleting, modifying, checking and other operations on it. This article introduces several methods of adding new elements, modifying element values, and deleting elements to a two-dimensional array. I hope it will be helpful to PHP developers.
The above is the detailed content of How to add value to two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!