How to delete a value from an array in php

PHPz
Release: 2023-04-27 10:16:59
Original
5019 people have browsed it

In PHP development, arrays are widely used. Sometimes we need to delete a specific value from an array. This operation may involve some internal structure operations of the array, but we can use some simple methods to achieve it.

This article will introduce several methods to delete a certain value in the PHP array. We will explain it from the following aspects: Method 1: Use the array_filter() method to delete a certain value in the array; Method 2 : Use the unset() method to delete a certain value in the array; Method 3: Use the array_diff() method to delete a certain value in the array; Method 4: Loop through the array to delete a certain value in the array. Let's take a look next.

Method 1: Use the array_filter() method to delete a certain value in the array

The array_filter() method is one of PHP's built-in functions and is one of PHP's powerful tools for processing arrays. . This function can filter the elements in the array that meet certain conditions and return a new array. When we want to delete a specific value in an array, we can use the array_filter() method to do so. The following is an example of using the array_filter() method to delete a certain value:

$array = array("apple", "banana", "orange", "banana", "cucumber");

$new_array = array_filter($array, function($value) {
    return $value != "banana";
});

print_r($new_array);
Copy after login

In this example, we define a $array array, which contains the names of some fruits. We use the array_filter() method to filter out elements that are not “banana” and assign them to the $new_array variable. Finally, use the print_r() method to output the new array. The output result is as follows:

Array
(
    [0] => apple
    [2] => orange
    [4] => cucumber
)
Copy after login
Copy after login

You can see that all "banana" has been deleted from $new_array, leaving only a few other elements.

Method 2: Use the unset() method to delete a certain value in the array

Using the unset() method to delete a specific value in the array is a very common practice in PHP development. This method deletes a value in the array by specifying a key value in the array. The following is an example of using the unset() method to delete a certain value:

$array = array("apple", "banana", "orange", "banana", "cucumber");

$key = array_search("banana", $array);

if ($key !== false) {
    unset($array[$key]);
}

print_r($array);
Copy after login

In this example, we define a $array array and use the array_search() method to find "banana" in the $array array ” key value of the element. If the element is found, we remove it using the unset() method. Finally, we use the print_r() method to output the new array. The output result is as follows:

Array
(
    [0] => apple
    [2] => orange
    [3] => cucumber
)
Copy after login
Copy after login

You can see that the original $array array has been modified, and "banana" has been deleted.

Method 3: Use the array_diff() method to delete a certain value in the array

The array_diff() method is also one of the PHP built-in functions and is a very powerful array processing tool. This function can calculate the difference of arrays and return a new array. When we need to delete a specific value in the array, we can do this by taking the difference between this value and the original array. The following is an example of using the array_diff() method to delete a certain value:

$array = array("apple", "banana", "orange", "banana", "cucumber");

$new_array = array_diff($array, array("banana"));

print_r($new_array);
Copy after login

In this example, we define a $array array, which contains the names of some fruits. We use the array_diff() method to calculate the difference between $array and an array with a single element "banana" and assign it to the $new_array variable. Finally, we use the print_r() method to output the new array. The output result is as follows:

Array
(
    [0] => apple
    [2] => orange
    [4] => cucumber
)
Copy after login
Copy after login

You can see that all "banana" has been deleted from $new_array, leaving only a few other elements.

Method 4: Loop through the array to delete a certain value in the array

You can also delete a specific value in the PHP array by looping through the array. This method is relatively simple and highly robust. The following is an example of using a loop to traverse an array to delete a certain value:

$array = array("apple", "banana", "orange", "banana", "cucumber");

foreach ($array as $key => $value) {
    if ($value == "banana") {
        unset($array[$key]);
    }
}

print_r($array);
Copy after login

In this example, we define a $array array and use a foreach loop to traverse the array. In the loop body, we use if to determine whether the current item is "banana", and if so, use the unset() method to delete it from the array. Finally, we use the print_r() method to output the new array. The output result is as follows:

Array
(
    [0] => apple
    [2] => orange
    [3] => cucumber
)
Copy after login
Copy after login

As you can see, during the process of looping through the array, we successfully used the unset() method to delete the element "banana".

Conclusion

In PHP development, using arrays is a very basic skill. Deleting specific elements in an array is also a common operation in daily development. This article explains how to delete a value in a PHP array from four aspects, including using the array_filter() method, using the unset() method, using the array_diff() method and using a loop to traverse the array to delete. Readers can choose a method that suits them according to their own needs.

The above is the detailed content of How to delete a value 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template