Home >Backend Development >PHP Problem >How to remove a key from an array in php

How to remove a key from an array in php

PHPz
PHPzOriginal
2023-04-23 10:07:16979browse

In PHP, we can use the unset() function to remove a key in an array. This function can directly delete the specified key and its corresponding value. In some cases, we need to modify and adjust the array, one of which is to remove a key in the array. In this article, we will introduce in detail how to use the unset() function to remove a key from the PHP array.

1. Syntax of PHP unset function

unset() function is used to destroy specified variables, including keys in the array and their corresponding values. Its syntax format is as follows:

unset($array_name[key]);

Among them, $array_name represents the name of the array to remove a key, and key represents the name of the array key to be removed. If you want to remove multiple keys in an array, you can pass multiple keys in the unset() function.

2. Example of PHP unset function

In order to better understand the usage of unset() function, let’s take a look at the following PHP sample code:

<?php
  $fruits = array(
    "apple" => "苹果",
    "orange" => "橙子",
    "banana" => "香蕉",
    "watermelon" => "西瓜",
    "grape" => "葡萄"
  );

  echo "未删除某个键前的数组:<br>";
  print_r($fruits);
  echo "<br><br>";

  unset($fruits["banana"]);
  
  echo "删除某个键后的数组:<br>";
  print_r($fruits);
?>

In the above In the code, we define an associative array $fruits containing five elements. First, we print the array without deleting any keys, and use the unset function to remove the specified One key - banana. Finally, we print out the deleted array.

The output of this example is:

未删除某个键前的数组:
Array
(
    [apple] => 苹果
    [orange] => 橙子
    [banana] => 香蕉
    [watermelon] => 西瓜
    [grape] => 葡萄
)

删除某个键后的数组:
Array
(
    [apple] => 苹果
    [orange] => 橙子
    [watermelon] => 西瓜
    [grape] => 葡萄
)

As mentioned above, we can use the unset() function to delete a key in the PHP array. Likewise, we can also print the array using the print_r() function as in the above example code.

3. Notes

You need to pay attention to the following points when using the unset() function:

  1. The unset() function deletes the value of the specified variable. rather than the variable itself. Therefore, if there are multiple keys with the same value in the array, as long as one key is deleted by the unset() function, all keys with the same value will be deleted.
  2. After deleting a key in the array, the key will no longer exist, that is, the key and its corresponding value will no longer occupy the array space. If you pass a non-existing key name to the unset() function, nothing happens and no error occurs.
  3. If you want to delete all elements of a variable or array, you can use the unset() function directly, but please note that if the unset() function is applied to the same variable multiple times, it will make the variable completely in memory. disappear.

4. Conclusion

Through the introduction of this article, we can clearly understand how to use PHP's unset() function to remove a key in the array. This function is a very useful tool in PHP, which can be used to modify and adjust arrays, bringing us a lot of convenience. When we need to update the array regularly, delete specific values, or improve the efficiency of the array, using the unset() function will be a good choice.

The above is the detailed content of How to remove a key from an array in php. 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