Home > Article > Backend Development > php two-dimensional array delete
In PHP, two-dimensional arrays are often used to store tabular data or store related data in the form of key-value pairs. But sometimes we need to delete certain data from a two-dimensional array, so this article will introduce a variety of methods to delete two-dimensional array data.
array_splice() function can delete a certain piece of data from the array and return the deleted element. Its usage is as follows:
array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement = array() ]] ) : array
Among them, the $input parameter is the array that needs to be operated; the $offset parameter is the index position to start deleting; the $length parameter is the number of elements to be deleted; the $replacement parameter is Used to replace deleted elements. If the $length parameter is not specified, the deleted elements will start at the $offset position and end at the end of the array. If the $replacement parameter is not specified, an array of deleted elements is returned.
When deleting an element in a two-dimensional array, we need to specify the position and quantity to be deleted, so you can use the following code:
$array = array( array('id' => 1, 'name' => 'A'), array('id' => 2, 'name' => 'B'), array('id' => 3, 'name' => 'C'), array('id' => 4, 'name' => 'D') ); $index = 1; //要删除的索引位置 $len = 1; //要删除的元素个数 array_splice($array, $index, $len);
After executing the above code, the array $array will Modified to:
[ ['id' => 1, 'name' => 'A'], ['id' => 3, 'name' => 'C'], ['id' => 4, 'name' => 'D'] ]
The unset() function can be used to delete the element corresponding to the specified key name in the array:
unset($array[$key]);
When deleting an element in a two-dimensional array, we need to specify the key name to be deleted and the array in which it is located. For example, delete the record with id 2 in the following array:
$array = array( array('id' => 1, 'name' => 'A'), array('id' => 2, 'name' => 'B'), array('id' => 3, 'name' => 'C'), array('id' => 4, 'name' => 'D') ); $key = null; foreach ($array as $k => $v) { if ($v['id'] == 2) { $key = $k; //找到了要删除的键名 break; } } if ($key !== null) { unset($array[$key]); }
After executing the above code, the array $array will be modified to:
[ ['id' => 1, 'name' => 'A'], ['id' => 3, 'name' => 'C'], ['id' => 4, 'name' => 'D'] ]
array_filter() function can be used to filter elements in an array, that is, to delete elements in the array that do not meet the conditions. When the second parameter is not passed, elements with a value of false are deleted by default.
When deleting an element in a two-dimensional array, we need to use the callback function to perform judgment and filtering. For example, delete the record with id 2 in the following array:
$array = array( array('id' => 1, 'name' => 'A'), array('id' => 2, 'name' => 'B'), array('id' => 3, 'name' => 'C'), array('id' => 4, 'name' => 'D') ); $array = array_filter($array, function($v) { return $v['id'] != 2; //返回id不等于2的记录 });
After executing the above code, the array $array will be modified to:
[ ['id' => 1, 'name' => 'A'], ['id' => 3, 'name' => 'C'], ['id' => 4, 'name' => 'D'] ]
Summary
This article Introduced various methods of deleting two-dimensional array data in PHP, including array_splice() function, unset() function and array_filter() function. Different methods can be selected according to different needs, and the specific implementation methods can also be optimized and improved according to the actual situation. Of course, when using any method, be sure to pay attention to the key name and index position of the array to avoid unnecessary errors.
The above is the detailed content of php two-dimensional array delete. For more information, please follow other related articles on the PHP Chinese website!