Home >Backend Development >PHP Problem >How to delete an element from php array

How to delete an element from php array

PHPz
PHPzOriginal
2023-04-25 16:12:59679browse

PHP array is a frequently used data type. In our daily work, sometimes we need to delete an element or a group of elements from an array. PHP provides a variety of methods to implement this function, and you can choose the appropriate method according to the actual situation.

1. Use the unset() function

The unset() function can be used to delete the element with the specified subscript in the array. The usage method is as follows:

<?php
$arr = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;);
unset($arr[2]);

print_r($arr);
?>

The above code will output:

Array
(
    [0] => A
    [1] => B
    [3] => D
)

As can be seen from the output results, the element with the specified subscript can be deleted using the unset() function. It should be noted that using the unset() function can only delete elements with a specified subscript, but not elements with a specified value.

2. Use the array_splice() function

array_splice() function can be used to delete a specified element in the array, and can insert new elements after deletion. The usage method is as follows:

<?php
$arr = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;);
array_splice($arr, 2, 1);

print_r($arr);
?>

The above code will output:

Array
(
    [0] => A
    [1] => B
    [2] => D
)

As can be seen from the output result, the array_splice() function can be used to delete the element with the specified subscript, and the array index will be reordered after deletion. It should be noted that when using the array_splice() function, if there are multiple elements to be deleted, this can be achieved by specifying the third parameter. For example:

<?php
$arr = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;);
array_splice($arr, 1, 3);

print_r($arr);
?>

The above code will output:

Array
(
    [0] => A
    [1] => E
)

As can be seen from the output result, it is equivalent to deleting the elements with subscripts 1, 2, and 3. It should be noted that when using the array_splice() function to delete elements, you can also insert new elements into the array after deletion, for example:

<?php
$arr = array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;);
array_splice($arr, 1, 3, array(&#39;F&#39;, &#39;G&#39;));

print_r($arr);
?>

The above code will output:

Array
(
    [0] => A
    [1] => F
    [2] => G
    [3] => E
)

From the output The result can be seen that after deleting the elements with indexes 1, 2, and 3, new elements are inserted into the array.

3. Use array_filter() function

array_filter() function can be used to delete elements that meet specified conditions in the array. The usage method is as follows:

<?php
$arr = array(1, 2, 3, 4, 5);
$arr = array_filter($arr, function ($item) {
    return $item % 2 == 0;
});

print_r($arr);
?>

The above code will output :

Array
(
    [1] => 2
    [3] => 4
)

As can be seen from the output results, the array_filter() function can be used to filter out the elements in the array that meet the specified conditions, and return the elements that meet the conditions into a new array. It should be noted that when using the array_filter() function to delete elements, the subscripts of the original array will be retained.

In summary, PHP can use the unset() function, array_splice() function and array_filter() function to delete elements in an array. You can choose the appropriate method according to the actual situation.

The above is the detailed content of How to delete an element from php array. 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