Home >Backend Development >PHP Problem >How to delete subarray from two-dimensional array in php
PHP, as a widely used programming language, often needs to operate arrays, among which deleting array elements is a common operation. In PHP, a two-dimensional array refers to the value of each element in the array, which is also an array. We sometimes need to delete elements in it. In this article, we will introduce how to delete elements from a two-dimensional array in PHP.
1. Introduction to two-dimensional arrays
In PHP, an array is an ordered set of values, and each value can be assigned a unique key. When the value of an element is also an array, the array can be called a two-dimensional array. A two-dimensional array can be viewed as an array composed of several one-dimensional arrays.
The following is a simple two-dimensional array:
$students = array( array('name'=>'张三', 'age'=>20, 'gender'=>'男'), array('name'=>'李四', 'age'=>22, 'gender'=>'女'), array('name'=>'王五', 'age'=>24, 'gender'=>'男') );
The above two-dimensional array describes the name, age and gender information of three students. Each student's information is described by a one-dimensional array, and these one-dimensional arrays form a two-dimensional array. We can access and operate elements in the two-dimensional array in the following ways:
// 访问二维数组中的元素 echo $students[0]['name']; // 输出 '张三' // 修改二维数组中的元素 $students[1]['age'] = 23;
2. Delete elements in the two-dimensional array
Sometimes we need to delete an element from the two-dimensional array , PHP provides a variety of methods to achieve this operation.
1. Use the unset function
In PHP, you can use the unset function to delete specified elements from an array. For two-dimensional arrays, we can use the following method to delete elements:
unset($array[$i][$j]);
Among them, $i represents the subscript of the one-dimensional array where the element is to be deleted, and $j represents the two-dimensional array where the element is to be deleted. subscript. If you want to delete multiple elements, you can use multiple unset functions nested.
The following is an example that demonstrates how to use the unset function to delete elements in a two-dimensional array:
$students = array( array('name'=>'张三', 'age'=>20, 'gender'=>'男'), array('name'=>'李四', 'age'=>22, 'gender'=>'女'), array('name'=>'王五', 'age'=>24, 'gender'=>'男') ); // 删除第一个学生的信息 unset($students[0]); // 删除第二个学生的姓名信息 unset($students[1]['name']); // 输出剩余的学生信息 print_r($students);
In the above code, we first use the unset function to delete the first element in the two-dimensional array $students. One element, information about the first student. Then, we used the unset function to delete the name information of the second student in $students. Finally, the print_r function is used to output the remaining student information, and you can see that the deletion was successful.
2. Use the array_splice function
In addition to using the unset function, you can also use the array_splice function to delete specified elements from a two-dimensional array.
The usage of the array_splice function is as follows:
array_splice(array &$input, int $offset [, int $length [, mixed $replacement ]]): array
Among them, $input represents the array to be operated on, $offset represents the subscript from which to start deleting, $length represents the number of elements to be deleted, $ replacement represents the element that needs to be added to the array. When deleting elements, the $replacement parameter can be omitted.
For two-dimensional arrays, we can use the following method to delete elements:
array_splice($array[$i], $j, 1);
Among them, $array represents the two-dimensional array where the element is to be deleted, and $i represents the two-dimensional array where the element is to be deleted. The subscript of the one-dimensional array, $j represents the subscript of the two-dimensional array where the element is to be deleted.
The following is an example that demonstrates how to use the array_splice function to delete elements in a two-dimensional array:
$students = array( array('name'=>'张三', 'age'=>20, 'gender'=>'男'), array('name'=>'李四', 'age'=>22, 'gender'=>'女'), array('name'=>'王五', 'age'=>24, 'gender'=>'男') ); // 删除第一个学生的信息 array_splice($students, 0, 1); // 删除第二个学生的姓名信息 array_splice($students[1], 0, 1); // 输出剩余的学生信息 print_r($students);
In the above code, we first use the array_splice function to delete the first element in the two-dimensional array $students One element, information about the first student. Then, we used the array_splice function to delete the name information of the second student in $students. Finally, the print_r function is used to output the remaining student information, and you can see that the deletion was successful.
3. Summary
The two-dimensional array in PHP is an array composed of several one-dimensional arrays. Deleting elements in a two-dimensional array can be achieved using the unset function and array_splice function. Using the unset function to delete elements is simple and direct, but the memory usage of the array will not be reduced; when using the array_splice function to delete elements, you need to specify the subscript to start deleting and the number of elements to delete, which can be reduced through this function. The purpose of array memory usage.
The above is the detailed content of How to delete subarray from two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!