Home  >  Article  >  Backend Development  >  How to delete a key value in an array in php

How to delete a key value in an array in php

PHPz
PHPzOriginal
2023-04-24 14:49:51603browse

In PHP programming, we often encounter situations where we want to delete a key value in an array. In this article, we will explore ways to delete a key value from an array in PHP.

1. Use the unset() function

The unset() function is a PHP built-in function used to destroy a given variable. It can be used to delete a key value in an array.

Syntax:

unset($array[key]);

Here, $array represents the array to be operated on, and key represents the key value to be deleted.

Sample code:

<?php
// 定义一个数组
$data = array(
    &#39;name&#39; => '张三',
    'age' => 18,
    'gender' => '男'
);

// 删除指定键值
unset($data['gender']);

// 输出数组
print_r($data);
?>

The output result is:

Array
(
    [name] => 张三
    [age] => 18
)

2. Use the array_splice() function

array_splice() function is a very useful function in PHP Function that can insert and delete elements in an array. You can use this function to delete a key value in the array.

Syntax:

array_splice($array, $offset, $length);

Here, $array represents the array to be operated on; $offset represents the value of the element to be deleted The starting position, if it is a negative number, is calculated from the end of the array; $length represents the number of elements to be deleted.

Sample code:

<?php
// 定义一个数组
$data = array(
    &#39;name&#39; => '张三',
    'age' => 18,
    'gender' => '男'
);

// 删除指定键值
array_splice($data, array_search('gender', array_keys($data)), 1);

// 输出数组
print_r($data);
?>

The output result is:

Array
(
    [name] => 张三
    [age] => 18
)

3. Use the array_filter() function

array_filter() function is another useful function in PHP A function that filters elements in an array. You can use this function to delete specified elements from an array.

Syntax:

array_filter($array, function($v, $k) { return $k != 'key'; } , ARRAY_FILTER_USE_BOTH);

Here, $array represents the array to be operated on; function($v, $k) { return $k != 'key'; } is an anonymous function used to specify the conditions for deletion, where $k != 'key' represents the deletion key It is the element of 'key'; ARRAY_FILTER_USE_BOTH means filtering the key value at the same time.

Sample code:

<?php
// 定义一个数组
$data = array(
    &#39;name&#39; => '张三',
    'age' => 18,
    'gender' => '男'
);

// 删除指定键值
$data = array_filter($data, function($k) {
    return $k != 'gender';
}, ARRAY_FILTER_USE_KEY);

// 输出数组
print_r($data);
?>

The output result is:

Array
(
    [name] => 张三
    [age] => 18
)

4. Use the array_diff_key() function

array_diff_key() function is another useful function in PHP function, which can take the difference set from the array based on the key name. You can use this function to delete specified elements from an array.

Syntax:

array_diff_key($array, array_flip(array('key')));

Here, $array represents the array to be operated on; array_flip(array( 'key')) means converting the key name to be deleted into a value for subsequent difference set operations.

Sample code:

<?php
// 定义一个数组
$data = array(
    &#39;name&#39; => '张三',
    'age' => 18,
    'gender' => '男'
);

// 删除指定键值
$data = array_diff_key($data, array_flip(array('gender')));

// 输出数组
print_r($data);
?>

The output result is:

Array
(
    [name] => 张三
    [age] => 18
)

Summary

In PHP, it is very common to delete a key value in an array operate. This article introduces four methods: using the unset() function, using the array_splice() function, using the array_filter() function, and using the array_diff_key() function. Different methods have their own advantages and disadvantages, and you need to choose the method that suits you according to the usage scenario.

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