How to delete specified elements in php array

PHPz
Release: 2023-04-25 14:08:38
Original
445 people have browsed it

In PHP, array is a very useful data type and is very common during the development process. Sometimes there will be some elements in our array that we don't need, and we need to delete them from the array. In this article, we will discuss how to delete specified elements from an array in PHP.

1. Use the unset function to delete the specified element

In PHP, we can use the unset function to delete the specified element in the array. The use of the unset function is very simple. You only need to pass the key value corresponding to the element to be deleted. The following is a sample code:

$fruits = array('apple', 'banana', 'grape', 'orange');
unset($fruits[2]);
print_r($fruits);
Copy after login

The above code first creates an array named $fruits, then uses the unset function to delete the element (grape) with index 2 in the array, and finally uses the print_r function to output Modified array. The output result is as follows:

Array
(
    [0] => apple
    [1] => banana
    [3] => orange
)
Copy after login
Copy after login

You can see that the specified element Grape has been successfully deleted from the array.

2. Use the array_splice function to delete the specified element

In addition to using the unset function, we can also use the array_splice function to delete the specified element in the array. This function can delete any number of elements at a specified position and insert new elements at that position. The following is a sample code:

$fruits = array('apple', 'banana', 'grape', 'orange');
array_splice($fruits, 2, 1);
print_r($fruits);
Copy after login

The above code first creates an array named $fruits, then uses the array_splice function to delete 1 element (grape) starting from index 2 in the array, and finally uses The print_r function outputs the modified array. The output result is as follows:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login

You can see that the specified element Grape has been successfully deleted from the array.

3. Use the array_diff function to delete the specified element

In addition to the above two methods, we can also use the array_diff function to delete the specified element in the array. The array_diff function is used to calculate the difference of multiple arrays. If the same value exists in the specified array, it will be deleted. The following is a sample code:

$fruits = array('apple', 'banana', 'grape', 'orange');
$deleteFruits = array('grape');
$result = array_diff($fruits, $deleteFruits);
print_r($result);
Copy after login

The above code first creates an array named $fruits and an array of elements to be deleted named $deleteFruits, and then uses the array_diff function to calculate the $fruits array and remove it. array of the same values ​​in the $deleteFruits array, and use the print_r function to output the modified array. The output result is as follows:

Array
(
    [0] => apple
    [1] => banana
    [3] => orange
)
Copy after login
Copy after login

You can see that the specified element Grape has been successfully deleted from the array.

Summary:

This article introduces three methods of deleting specified elements in an array in PHP, including using the unset function, array_splice function and array_diff function. These methods are very simple and easy to use, and the method you choose depends on your actual needs.

The above is the detailed content of How to delete specified elements in php array. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!