Home  >  Article  >  Backend Development  >  How to delete array members in PHP (three methods)

How to delete array members in PHP (three methods)

PHPz
PHPzOriginal
2023-04-19 09:21:57605browse

In PHP, array is a very commonly used data structure, but when using arrays, it often involves deleting array members. Therefore, this article will introduce several different ways to delete array members in PHP, including the unset(), array_splice(), and array_filter() functions.

unset() function

The unset() function can be used to delete one or more specified members in the array. The syntax is as follows:

unset($array[key]);

Among them, $array represents the array to be operated on, and key represents the key of the member to be deleted. If you want to delete multiple members, you can use multiple unset() functions to delete each member separately. For example:

unset($array[key1]);
unset($array[key2]);

In addition, you can also use variables to specify the members to be deleted. For example:

$key = 'myKey';
unset($array[$key]);

It should be noted that after using the unset() function to delete an array member, the memory space occupied by the member will not be released, but will be marked as recyclable. If you want to completely free up memory space, you can use the array_values() function to reindex the array.

array_splice() function

array_splice() function can be used to delete a certain number of members at a specified position in the array and return the deleted members. The syntax is as follows:

array_splice($array, $offset, $length);

Among them, $array represents the array to be operated on, $offset and $length respectively represent the starting position to be deleted and the number of members to be deleted. For example:

$removed = array_splice($array, 2, 3);

means starting from the 3rd position of the $array array, delete 3 members, and save these deleted members in the $removed array.

It should be noted that after using the array_splice() function to delete an array member, the memory space occupied by the member will be released, and the index of the array will be automatically rebuilt.

array_filter() function

The array_filter() function can be used to filter array members, and can also be used to delete members that do not meet the conditions. The syntax is as follows:

array_filter($array, $callback);

Among them, $array represents the array to be operated on, and $callback is a callback function, which can be used to define filtering conditions. For example, you can use an anonymous function to delete members with a value of 0:

$array = array(1, 0, 3, 0, 5);
$array = array_filter($array, function($value) {
    return $value != 0;
});

After this operation, all members with a value of 0 in the $array array will be deleted, leaving only 1, 3 and 5. value.

It should be noted that after using the array_filter() function to delete array members, the memory space occupied by the deleted members will be released, and the index of the array will be automatically rebuilt.

Summary

The above are three common methods of deleting array members in PHP. They each have their own characteristics. You can choose according to the actual situation when using them. Among them, the unset() function is the simplest method, but it does not release memory space; the array_splice() function can release memory space, but it needs to specify the starting position and the number of deletions; the array_filter() function can perform complex filtering operations based on the callback function . Therefore, when using these functions, you need to choose based on your specific needs.

The above is the detailed content of How to delete array members in PHP (three methods). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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