Home >Backend Development >PHP Problem >delete entire array in php
In PHP programming, array is a very common data structure, and at the same time, the operation of an array is also often used. Deleting an entire array is a common operation that allows us to free up the memory occupied by the array and prevent the data stored in the array from existing in the computer's memory. In order to better understand how to delete the entire array, this article will introduce it from the following aspects.
In PHP, creating an array is very simple. You can use the array() function or [] to create an empty array, for example:
$arr = array(); $arr = [];
Arrays can also be initialized when declared, for example:
$arr = array(1, 2, 3); $arr = [1, 2, 3];
Before understanding how to delete an entire array, We need to first understand how to delete a single array element. PHP provides a variety of methods to delete individual elements in an array, among which the unset() function and the array_splice() function are more commonly used.
unset($arr[$key])
where $key is the subscript of the element to be deleted, for example:
$arr = [1, 2, 3]; unset($arr[1]); // 删除下标为1的元素 print_r($arr); // 输出[1, 3]
array_splice($arr, $offset, 1);
where $offset is the subscript of the element to be deleted, for example:
$arr = [1, 2, 3]; array_splice($arr, 1, 1); // 删除下标为1的元素 print_r($arr); // 输出[1, 3]
It should be noted that after using the unset() function to delete an array element, the array subscript occupied by the element still exists, but the value at that position is deleted. After using the array_splice() function to delete array elements, the array subscripts will be rearranged. You can reset the array subscripts through the array_values() function, for example:
$arr = [1, 2, 3]; unset($arr[1]); // 删除下标为1的元素 $arr = array_values($arr); // 重置数组下标 print_r($arr); // 输出[1, 3]
If you need to delete the entire array, PHP provides the unset() function, the usage format is as follows:
unset($arr);
Among them, $arr is the array to be deleted.
For example:
$arr = [1, 2, 3]; unset($arr); // 删除整个数组 print_r($arr); // 报错: Undefined variable: arr
It should be noted that after deleting the array, the variable will no longer point to an array, and the occupied memory will be released by the system.
When deleting an array, you need to pay special attention to avoid accidentally deleting other computer resources. Therefore, when using the unset() function to delete an array, you can use the isset() function to determine whether the variable exists, for example:
if (isset($arr)) { unset($arr); }
In addition, you can also use the is_array() function to determine whether the variable is a Array, for example:
if (is_array($arr)) { unset($arr); }
In addition, after using the unset() function to delete the array, it is best to assign the variable to null, for example:
$arr = [1, 2, 3]; unset($arr); // 删除整个数组 $arr = null; // 将该变量赋值为null
This can ensure that the variable no longer points to an array , thereby avoiding misoperation.
To summarize, this article explains how to create an array, and how to delete individual array elements and entire arrays. When performing deletion operations, special attention needs to be paid to avoid accidentally deleting other computer resources. At the same time, it is best to assign the variable to null to ensure that the variable no longer points to an array.
The above is the detailed content of delete entire array in php. For more information, please follow other related articles on the PHP Chinese website!