Home  >  Article  >  Backend Development  >  How to remove elements from php array

How to remove elements from php array

藏色散人
藏色散人Original
2021-03-22 09:51:103971browse

Methods for removing elements from php arrays: 1. Remove array elements through "unset($array[1]);"; 2. Through "array_splice($array, 1, 1);" method to remove array elements.

How to remove elements from php array

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

Introduction

PHP's array, to put it bluntly, is a mapped set of key-value pairs. So how to remove elements from array elements? You must first think of traversing the array, then finding the target item, and then deleting it.

I saw that some students set the element to be deleted = null, which does not work because null also exists as a value. Elements cannot be removed.

This article will specifically talk about the method of removing array elements.

Learning time

Roughly speaking, there are two common methods for removing array elements, one is the unset function and the other is the array_splice function.

array_splice When to use? That is, you get the value but don't know its index, so you need to use this.

unset function

Please note that using the unset function will not rebuild the index and the array will not be shuffled. If you want to re-create the index after deleting an element, do array_values ​​and the returned array will be re-indexed. And all indexes are set to numbers starting from 0 by default.

Example:

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);

After performing this operation, the value of the array $array is as follows:

[
    [0] => a
    [2] => c
]

You can notice that the array index has not changed.

[Recommended learning: PHP video tutorial]

array_splice function

This function associates the array after removing elements The corresponding relationship will not change. The parameter form of this function:

array array_splice( array &$input, int $offset[, int $length = 0[, mixed $replacement]] )

is used to remove part of the array and replace it with other values. If the last parameter replaement is not passed, it defaults to starting with offset. If it is a numeric index, the returned array will be re-indexed:

$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);

Returned value:

[
    [0] => a
    [1] => c
]

You can see The index has been reindexed.

Go one step further

The two examples mentioned above are both deletions of single elements. Let's talk about removing multiple array elements.

unset / arraysplice is too cumbersome to use, you have to operate one by one. If we know the key or value of the array element, we can use array_diff / array_diff_key to perform batch operations.

array_diff function

For example, if the value of the element to be deleted is known, it can be removed as follows:

$array = [0 => "a", 1 => "b", 2 => "c"];

$array = array_diff($array, ["a", "c"]);

<br/>

Result output :

[
    [1] => b
]

The index of the array will not be rebuilt.

array_diff_keys function

If the index of the element to be deleted is known, it can be deleted as follows:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "xy", "2" => "xy"]);

Output after execution:

[
    [1] => b
]

The index is not rebuilt .

Written at the end

In this article, we use several examples to demonstrate how to remove elements from an array, so there is no need to go around the world with a for loop. PHP provides many functional tools that are helpful in reducing redundant code, and we should use them.

The above is the detailed content of How to remove elements 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