php delete key value of array

PHPz
Release: 2023-05-22 20:45:36
Original
402 people have browsed it

PHP is a very popular web development language, and arrays are one of the most commonly used data structures in PHP. When working with arrays, sometimes you need to delete one or more keys in the array and rearrange and re-index the remaining keys. This article will introduce how to delete array key values ​​in PHP.

  1. unset() function

The unset() function in PHP can be used to delete one or more variables, including array key values. If you delete only a key value in the array, the element where the key value is located will be deleted, and the key values ​​​​of other elements will not be re-indexed.

The following is sample code:

<?php
$fruits = array("apple", "banana", "cherry");
unset($fruits[1]);  // 删除键值为 1 的元素(即 banana)
print_r($fruits);   // 输出: Array ( [0] => apple [2] => cherry )
?>
Copy after login

There are three elements in the $fruits array, whose key values ​​are 0, 1 and 2 respectively. After using the unset() function to remove the element with the key value 1 (i.e. "banana"), the output only has two elements with the key values ​​0 and 2.

If you want to delete multiple key values, you can use commas to separate them, or put the key values ​​as values ​​into an array.

The following is the sample code:

<?php
$fruits = array("apple", "banana", "cherry", "durian");
unset($fruits[1], $fruits[3]);  // 删除键值为 1 和 3 的元素(即 banana 和 durian)
print_r($fruits);               // 输出: Array ( [0] => apple [2] => cherry )
 
unset($fruits[0], $fruits[2]);  // 删除键值为 0 和 2 的元素(即 apple 和 cherry)
print_r($fruits);               // 输出: Array ( )
 
$fruits = array("apple", "banana", "cherry", "durian");
unset($fruits[1], $fruits[3]);  // 删除键值为 1 和 3 的元素(即 banana 和 durian)
print_r(array_values($fruits)); // 输出: Array ( [0] => apple [1] => cherry )
?>
Copy after login

There are four elements in the $fruits array, and their key values ​​are 0, 1, 2 and 3 respectively. After using the unset() function to delete elements with key values ​​1 and 3 respectively, there are only two elements in the output result with key values ​​0 and 2.

If you want to re-index the remaining elements, you can use the array_values() function, which returns a new array with all key values ​​in order.

  1. array_splice() function

The array_splice() function in PHP can be used to delete one or more elements in an array and re-index the remaining elements . The first parameter of this function is the array to be operated on, the second parameter is the position to start deleting elements, the third parameter is the number of elements to be deleted, and the fourth parameter is the optional new element to be inserted.

The following is the sample code:

<?php
$fruits = array("apple", "banana", "cherry", "durian");
array_splice($fruits, 1, 2);  // 从位置 1 开始删除 2 个元素(即 banana 和 cherry)
print_r($fruits);            // 输出: Array ( [0] => apple [1] => durian )
?>
Copy after login

There are four elements in the $fruits array, and their key values ​​are 0, 1, 2 and 3 respectively. After using the array_splice() function to remove 2 elements (i.e. "banana" and "cherry") starting from position 1, the output only has two elements with key values ​​0 and 1.

If you want the remaining elements to be re-indexed, you can use the array_values() function before deleting the elements.

  1. array_filter() function

The array_filter() function in PHP can be used to filter elements in an array and return a new array (by default, the original array will not be modified). The first parameter of this function is the array to filter, and the second parameter is an optional callback function that tests each element and returns true or false.

The following is the sample code:

<?php
$fruits = array("apple", "banana", "cherry", "durian");
$fruits = array_filter($fruits, function($value, $key) {
    return $key != 1 && $key != 2;  // 删除键值为 1 和 2 的元素(即 banana 和 cherry)
}, ARRAY_FILTER_USE_BOTH);
print_r($fruits);  // 输出: Array ( [0] => apple [3] => durian )
?>
Copy after login

There are four elements in the $fruits array, and their key values ​​are 0, 1, 2 and 3 respectively. After using the array_filter() function and callback function to remove elements with key values ​​1 and 2, the output only has two elements with key values ​​0 and 3.

If you want the remaining elements to be re-indexed, you can use the array_values() function after using the array_filter() function.

Summary

The above are three ways to delete array key values ​​in PHP. Choosing the appropriate method based on the situation can improve the efficiency and readability of your code. Note: When using the unset() function or array_splice() function, the deleted key value will no longer exist in the array, while when using the array_filter() function, the array will be re-indexed.

The above is the detailed content of php delete key value of array. 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