How to delete specified value in array in php

PHPz
Release: 2023-04-21 09:14:30
Original
455 people have browsed it

Today we will discuss how to write a method to delete specified values ​​in an array using PHP.

First, we need to understand arrays. Array is a data type that allows us to store multiple values ​​in one variable. Arrays in PHP can be indexed arrays and associative arrays. Indexed arrays are numerically indexed arrays, while associative arrays use string keys to specify each value. We can create an array using:

// 索引数组
$numbers = array(1, 2, 3, 4, 5);

// 关联数组
$person = array("name" => "John", "age" => 30, "city" => "New York");
Copy after login

Now, let’s see how to delete a specific value in an array. Assume our array is an indexed array:

$numbers = array(1, 2, 3, 4, 5);
Copy after login

We want to delete the value 3 in the array, we can use PHP's built-in function array_search() to find the key name of the value, and use unset() function to delete the value.

$index = array_search(3, $numbers);
unset($numbers[$index]);
Copy after login

Now, if we print the $numbers array, you will see that it does not contain the value 3 anymore.

print_r($numbers);
// 输出:Array ( [0] => 1 [1] => 2 [3] => 4 [4] => 5 )
Copy after login

However, when we use the array_search() function, it can only return the key of the first match found. If there are multiple values ​​in the array, we need to remove each match. To handle this situation we can use a loop to go through the array and remove all matches.

$numbers = array(1, 2, 3, 4, 5, 3);
$search = 3;

foreach ($numbers as $key => $value) {
    if ($value == $search) {
        unset($numbers[$key]);
    }
}

print_r($numbers);
// 输出:Array ( [0] => 1 [1] => 2 [3] => 4 [4] => 5 )
Copy after login

In this way, we delete all elements with a value of 3 in the array.

In addition, if the array is an associative array, we can use the array_search() function in the same way as the unset() function to delete the specified value.

$person = array(
    "name" => "John",
    "age" => 30,
    "city" => "New York"
);

$search = "John";

foreach ($person as $key => $value) {
    if ($value == $search) {
        unset($person[$key]);
    }
}

print_r($person);
// 输出:Array ( [age] => 30 [city] => New York )
Copy after login

In this way, we successfully deleted the key with the value "John" in the associative array.

To summarize, we can use the array_search() function and the unset() function to delete the specified value in the PHP array. If there are multiple values ​​in the array that need to be deleted, we can use a loop to go through and delete all matching items. This method applies to both indexed and associative arrays. I hope this article helped you better understand the basics and methods of PHP arrays.

The above is the detailed content of How to delete specified value in 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