How to delete key-value pairs in an array in php (two methods)

PHPz
Release: 2023-04-18 14:17:29
Original
953 people have browsed it

In PHP, array is a very practical data structure, often used to store and operate large amounts of data. When performing array operations, it is sometimes necessary to delete certain key-value pairs in order to maintain the cleanliness and readability of the array. This article will introduce two common methods of deleting array key-value pairs.

Method 1: unset function

The unset function is a function in PHP used to delete variables or array elements. It can completely eliminate the existence of a certain key-value pair in the array, achieving Delete effect. The following is a sample code that uses the unset function to delete array key-value pairs:

<?php
// 创建一个关联数组
$fruit = array(
    &#39;apple&#39;  => '苹果',
    'banana' => '香蕉',
    'orange' => '橙子',
    'pear'   => '梨子'
);

// 删除键名为"orange"的键值对
unset($fruit['orange']);

// 打印输出数组中的键值对
print_r($fruit);
?>
Copy after login

In the above code, we first created an associative array named $fruit and added four sets of key-value pairs. Next, use the unset function to delete the key-value pair with the key name "orange". Finally, the updated $fruit array is printed out using the print_r function.

The following is the output result:

Array
(
    [apple] => 苹果
    [banana] => 香蕉
    [pear] => 梨子
)
Copy after login

As can be seen from the output result, the key-value pair with the key name "orange" has been deleted, and only three items containing three are left in the $fruit array. Group data of key-value pairs.

Method 2: array_diff_key function

The array_diff_key function is a function in PHP used to compare the differences of multiple arrays. It accepts two or more arrays as parameters and returns the difference between these arrays. A new array composed of key names that differ between If you compare this function with an empty array, you can achieve the effect of deleting the array key-value pairs. The following is a sample code that uses the array_diff_key function to delete array key-value pairs:

<?php
// 创建一个关联数组
$colors = array(
    &#39;red&#39;   => '红色',
    'green' => '绿色',
    'blue'  => '蓝色',
    'purple'=> '紫色'
);

// 删除键名为"blue"的键值对
$colors = array_diff_key($colors, array('blue' => ''));

// 打印输出数组中的键值对
print_r($colors);
?>
Copy after login

In the above code, we first created an associative array named $colors and added four sets of key-value pairs. Next, use the array_diff_key function to compare the difference between the $colors array and an empty array with a key name of "blue" and an empty key value, and overwrite the comparison result into the $colors array. Finally, the updated $colors array is printed out using the print_r function.

The following is the output result:

Array
(
    [red] => 红色
    [green] => 绿色
    [purple] => 紫色
)
Copy after login

As can be seen from the output result, the key-value pair with the key name "blue" has been deleted, and only three items containing three are left in the $colors array. Group data of key-value pairs.

Summary

This article introduces two common methods of deleting array key-value pairs in PHP: using the unset function and the array_diff_key function. The unset function can directly delete a key-value pair, while the array_diff_key function can achieve the effect of deletion by comparing array differences. The above two methods each have their own advantages and disadvantages, and they need to be flexibly chosen according to needs in actual development.

The above is the detailed content of How to delete key-value pairs in an array in php (two methods). 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