How to delete specified elements from an array in php

PHPz
Release: 2023-04-26 09:29:20
Original
1199 people have browsed it

In PHP development, arrays are a very common data structure. When using arrays, sometimes we need to delete an element in the array. PHP provides some methods to delete specified elements from an array. This article will introduce you to PHP's method of deleting specified elements from an array.

1. Use the unset function to delete elements

PHP’s unset function can be used to delete a variable or an element in an array. The specific usage is as follows:

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

Among them, $array is the array of elements to be deleted, and index is the index specifying the element to be deleted.

For example, the code is as follows:

$arr = array('one', 'two', 'three', 'four', 'five'); unset($arr[2]); print_r($arr);
Copy after login

The running result is:

Array ( [0] => one [1] => two [3] => four [4] => five )
Copy after login
Copy after login

It can be seen that after deleting elements in the array through the unset function, the index position of the deleted element is Once empty, the index of the original array will be rebuilt.

2. Use the array_splice function to delete elements

PHP’s array_splice function can insert or delete specified elements or a range of elements in an array. Its usage is:

array_splice($input, $offset, $length, $replacement);
Copy after login

where $input is the array to be operated on, $offset is the index of the starting position, $length is the number of subsequent elements to delete, and $replacement is the optional replacement element. array.

For example, the code is as follows:

//删除一个元素 $arr = array('one', 'two', 'three', 'four', 'five'); array_splice($arr, 2, 1); print_r($arr); //删除多个元素 $arr = array('one', 'two', 'three', 'four', 'five'); array_splice($arr, 1, 2); print_r($arr);
Copy after login

The running result is:

Array ( [0] => one [1] => two [2] => four [3] => five ) Array ( [0] => one [1] => four [2] => five )
Copy after login

It can be seen that after using the array_splice function, the index of the original array will not be reconstructed, but Leave it as is.

3. Use the array_diff function to delete elements

PHP’s array_diff function can compare the values of two or more arrays and return different values. We can use this function to delete a specified element. The specific usage is as follows:

$array = array_diff($array, array($value));
Copy after login

Among them, $array is the array of elements to be deleted, and $value is the specified value to be deleted.

For example, the code is as follows:

$arr = array('one', 'two', 'three', 'four', 'five'); $arr = array_diff($arr, array('three')); print_r($arr);
Copy after login

The running result is:

Array ( [0] => one [1] => two [3] => four [4] => five )
Copy after login
Copy after login

It can be seen that after using the array_diff function, the index of the original array will also be rebuilt.

The above are the three methods of PHP to delete the elements specified in the array. Different methods are suitable for different scenarios. You can choose the appropriate method to delete specified elements in the array according to the actual situation.

The above is the detailed content of How to delete specified elements from an 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!