php delete current array

王林
Release: 2023-05-19 16:09:38
Original
395 people have browsed it

PHP is a popular programming language with a wide range of application scenarios, especially in web development. In PHP, an array is a common data type used to store and manipulate a series of related data. However, in some cases, we may need to delete elements from the current array. This article will introduce how to delete the current array using PHP.

1. Delete array elements through the unset() function

PHP provides a built-in unset() function, which can be used to delete elements in the array. To delete an element from the current array, we can use the unset() function and pass the key name of the element to be deleted as the parameter of the function. Here is an example:

$fruits = array('apple', 'banana', 'orange', 'pear'); unset($fruits[1]); // 删除数组中索引为1的元素 print_r($fruits); // 输出:Array([0] => apple [2] => orange [3] => pear)
Copy after login

In this example, $fruits is an array containing four elements. We removed the element with index 1 (i.e. 'banana') using the unset() function. Then, we print the array using the print_r() function and we can see that the deleted element is no longer present in the array.

Please note that after using the unset() function to delete an array element, the space occupied by the element will not be released. This means that if we try to re-add a value using that key name, the previous value will be overwritten.

2. Delete array elements through array_splice() function

Another PHP function that can delete elements in an array is the array_splice() function. This function is very convenient for deleting array elements and also allows you to re-index the array after deleting the element. Here is the example:

$fruits = array('apple', 'banana', 'orange', 'pear'); array_splice($fruits, 1, 1); // 删除数组中索引为1的元素 print_r($fruits); // 输出:Array([0] => apple [1] => orange [2] => pear)
Copy after login

In this example, we have used the array_splice() function to remove the second element in the array (i.e. 'banana'). The parameters of the function are as follows:

  • $fruits: the array to be operated on;
  • 1: the starting index position of the element to be deleted;
  • 1: The number of elements to delete.

When using the array_splice() function, please note that it does not work with associative arrays. If we try to use it in an associative array, the keys of the elements will be lost and only the values of the elements will remain.

3. Use the unset() function to delete the last element of the array

If we only need to delete the last element of the current array, we can use the array_pop() function or the unset() function. The difference between these two functions is that the array_pop() function returns the deleted elements, while the unset() function does not return any value. Here is the example:

$fruits = array('apple', 'banana', 'orange', 'pear'); unset($fruits[count($fruits)-1]); // 删除数组的最后一个元素 print_r($fruits); // 输出:Array([0] => apple [1] => banana [2] => orange)
Copy after login

In this example, we have used the unset() function to delete the last element of the array (i.e. 'pear'). We can choose to delete the last element of the array by calculating the length of the array and using a specific index. Unlike the array_splice() function, the unset() function can be used with any type of array, including associative arrays and numerically indexed arrays.

To sum up, we can use the unset() function and array_splice() function to delete elements in the current array. You can also use the array_pop() function or the unset() function to delete the last element of the array. According to the specific needs, just select the appropriate function to operate.

The above is the detailed content of php delete current array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!