Home  >  Article  >  Backend Development  >  PHP removes a certain key value from an array

PHP removes a certain key value from an array

PHPz
PHPzOriginal
2023-05-06 14:16:08641browse

When developing PHP applications, it is very common to manipulate arrays. Sometimes, we need to delete a key value of an array to meet specific business needs. In this article, we will explore how to easily remove a key value from an array using PHP language.

1. Use the unset() function in PHP to delete the key value of the array

The PHP language provides a powerful and flexible function unset(), which can be used to delete a certain key value of the array. . In the following example, we will use the unset() function to delete a certain key value in the array.

Code example:

<?php
// 创建数组
$fruit = array(
    "apple" => "red",
    "banana" => "yellow",
    "orange" => "orange"
);

// 删除数组中的一项
unset($fruit["banana"]);

// 输出修改后的数组
print_r($fruit);
?>

Output result:

Array
(
    [apple] => red
    [orange] => orange
)

By using the unset() function, we can easily delete a key value in the array, thus achieving Modifications to the array.

2. Use the array_diff_key() function in PHP to delete the array key value

Another way to delete the array key value is to use the array_diff_key() function of PHP. This function removes key values ​​matching the specified key from the array. In the following example, we will use the array_diff_key() function to delete the specified key value in the array.

Code example:

<?php
// 创建数组
$colors = array(
    "red" => 1,
    "green" => 2,
    "blue" => 3
);

// 创建需要删除的键值数组
$keys = array(
    "red" => "",
    "green" => ""
);

// 用array_diff_key()函数删除数组中的指定键值
$result = array_diff_key($colors, $keys);

// 输出修改后的数组
print_r($result);
?>

Output result:

Array
(
    [blue] => 3
)

In this example, we use the array $keys to represent the key value that needs to be deleted, and then use array_diff_key() The function removes the key value matching $keys from the array $colors. Finally, the modified array $colors is output, with only the key value "blue" retained.

3. Use "unset foreach" or "array_filter" to delete array key values

In addition to the above two methods, we can also use "unset foreach" or "array_filter" to delete arrays a certain key value. In the following example, we will show how to use these two methods to achieve the effect of deleting array keys.

Example 1, use "unset foreach" to delete array key values:

Code example:

<?php
// 创建数组
$animals = array(
    "dog" => 1,
    "cat" => 2,
    "tiger" => 3
);

// 删除想要的键值
foreach ($animals as $key => $value) {
    if ($key == "cat") {
        unset($animals[$key]);
    }
}

// 输出修改后的数组
print_r($animals);
?>

Output result:

Array
(
    [dog] => 1
    [tiger] => 3
)

In this example, We use a foreach loop to traverse the array $animals. When the key value is "cat", we use the unset() function to delete the key value. Finally, the modified array $animals is output, with only the two key values ​​​​of "dog" and "tiger" retained.

Example 2, use "array_filter" to delete array key values:

Code example:

<?php
// 创建数组
$numbers = array(
    "one" => 1,
    "two" => 2,
    "three" => 3
);

// 删除想要的键值
$result = array_filter($numbers, function($key) {
    return ($key != "two");
}, ARRAY_FILTER_USE_KEY);

// 输出修改后的数组
print_r($result);
?>

Output result:

Array
(
    [one] => 1
    [three] => 3
)

In this example, we The array_filter() function in PHP is used to only retain elements whose key value is not "two" by passing a callback function. Finally, the modified array $numbers is output, with only the two key values ​​"one" and "three" retained.

Summary

This article introduces various methods to delete a certain key value of the array. Using the unset() function is the most common method, while using the array_diff_key() function can only delete key values ​​​​that match the specified array from the array. At the same time, you can also use "unset foreach" and "array_filter" to delete a key value of the array. Choosing the most appropriate method according to the specific situation can operate the array more efficiently.

The above is the detailed content of PHP removes a certain key value from an array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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