Home > Backend Development > PHP Problem > How to remove specified subscript array in php

How to remove specified subscript array in php

PHPz
Release: 2023-04-20 14:53:16
Original
824 people have browsed it

php Remove the specified subscript array

In PHP, we often need to operate on the array. One of the common operations is to remove the element with the specified subscript. This article will introduce several methods to remove specified subscript arrays in PHP.

Method 1: unset() function

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

unset($array[$key]);
Copy after login

where $array is the element to be deleted array, $key is the subscript to be deleted.

Sample code:

// 创建数组
$array = array('a', 'b', 'c', 'd', 'e');

// 删除数组中下标为 2 的元素
unset($array[2]);

// 输出结果
print_r($array);
Copy after login

Running result:

Array
(
    [0] => a
    [1] => b
    [3] => d
    [4] => e
)
Copy after login

You can see that the element with index 2 in the array has been removed.

Method 2: array_splice() function

array_splice() function can delete and replace an element in the array. The syntax is as follows:

array_splice($array, $start, $length, $replacement);
Copy after login

where $array is the operation to be performed An array, $start is the starting subscript, $length is the number of elements to be deleted, and $replacement is the array of elements to be replaced. If the $replacement parameter is empty, only the element will be deleted without replacement.

Sample code:

// 创建数组
$array = array('a', 'b', 'c', 'd', 'e');

// 删除数组中下标为 2 的元素
array_splice($array, 2, 1);

// 输出结果
print_r($array);
Copy after login

Running result:

Array
(
    [0] => a
    [1] => b
    [2] => d
    [3] => e
)
Copy after login

You can see that the element with index 2 in the array has been removed.

Method 3: array_diff_key() function

array_diff_key() function can be used to compare the key names of two arrays and return different key names. The syntax is as follows:

array array_diff_key ( array $array1 , array $array2 [, array $... ] )
Copy after login

Where $array1 and $array2 are the two arrays to be compared, and the returned array contains the key names in the first array that do not exist in any other parameter array.

Sample code:

// 创建数组
$array1 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry');
$array2 = array('b' => 'banana');

// 删除数组中下标为 b 的元素
$array = array_diff_key($array1, $array2);

// 输出结果
print_r($array);
Copy after login

Running result:

Array
(
    [a] => apple
    [c] => cherry
)
Copy after login

You can see that the element with subscript b in the array has been removed.

Method 4: Use for loop

We can use for loop to traverse the array and manually delete elements with specified subscripts.

Sample code:

// 创建数组
$array = array('a', 'b', 'c', 'd', 'e');

// 删除数组中下标为 2 的元素
for($i = 0; $i < count($array); $i++) {
    if($i == 2) {
        unset($array[$i]);
    }
}

// 输出结果
print_r($array);
Copy after login

Running result:

Array
(
    [0] => a
    [1] => b
    [3] => d
    [4] => e
)
Copy after login

You can see that the element with index 2 in the array has been removed.

Summary

This article introduces several methods to remove specified subscript arrays in PHP, including using the unset() function, array_splice() function, array_diff_key() function and for loop. Different methods are suitable for different scenarios, and developers can choose the appropriate method according to the actual situation.

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