Home > Backend Development > PHP Problem > How to delete elements from array in PHP

How to delete elements from array in PHP

PHPz
Release: 2023-04-10 10:11:48
Original
525 people have browsed it

In PHP programming, array is a very important data structure. The size of the array is variable, and elements can be continuously added to it. However, sometimes it is necessary to delete elements from an array to ensure the data integrity of the array.

This article will introduce how to delete elements in an array in PHP. We'll explore methods of removing single elements and multiple elements, and discuss the pros and cons of each method.

1. Delete a single element

In PHP, you can use the unset function to delete a single element in an array. The unset function is a special function in PHP used to delete variables. It deletes one or more variables and frees the memory space they occupy.

The following is a sample code that uses the unset function to delete a single element:

<?php
$numbers = array(1, 2, 3, 4, 5);
unset($numbers[2]);
print_r($numbers);
?>
Copy after login

Run the above code, the output is as follows:

Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [4] => 5
)
Copy after login

In the code, we define an array $numbers , which contains 5 elements. Then, we use the unset function to delete the third element in the $numbers array (the element with index 2). Finally, we use the print_r function to output the $numbers array after removing elements.

It should be noted that when using the unset function to delete an element in an array, the subscript of the element will no longer exist. In the above example, we have deleted the element with index 2, so the elements in the $numbers array have indexes 0, 1, 3, 4.

2. Delete multiple elements

In PHP, to delete multiple array elements at the same time, you can use the array filter (array_filter) function. The array filter function is used to iterate over the elements in the array, execute a callback function, and then use the return value of the callback function as the elements of the new array. Elements whose callback function returns false will be deleted.

The following is a sample code that uses the array filter function to delete multiple elements:

<?php
$numbers = array(1, 2, 3, 4, 5);
$indexes = array(1, 3);

$filtered = array_filter($numbers, function($key) use ($indexes) {
    return !in_array($key, $indexes);
}, ARRAY_FILTER_USE_KEY);

print_r($filtered);
?>
Copy after login

Run the above code, the output is as follows:

Array
(
    [0] => 1
    [2] => 3
    [4] => 5
)
Copy after login

In the code, we define a The array $numbers, which contains 5 elements, also defines an array $indexes, which contains the indexes of the elements to be deleted. Then, we use the array filter function to traverse the $numbers array, filter elements that meet the conditions, and finally return a new array.

Using the array filter function to delete multiple elements can be more flexible and decide which elements to delete based on a variety of conditions.

Summary:

  • In PHP, use the unset function to delete a single array element.
  • Use the array filter function to delete multiple array elements.
  • When deleting a single element, using the unset function is efficient, but multiple elements cannot be deleted at the same time.
  • When deleting multiple elements, using the array filter function is more flexible. You can delete multiple elements at once based on multiple conditions.

The above is the detailed content of How to delete elements from array in PHP. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template