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.
- 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 ) ?>
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 ) ?>
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.
- 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 ) ?>
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.
- 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 ) ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
