Home  >  Article  >  Backend Development  >  How to clear numeric key names in array in php

How to clear numeric key names in array in php

PHPz
PHPzOriginal
2023-03-27 15:29:35607browse

When developing PHP applications, sometimes we need to perform some operations on the array, including deleting numeric key names in the array. The purpose of this operation is usually to continuously increment the key names in the array, rather than leaving numeric key names that jump in the middle.

In this article, we will learn how to remove numeric key names in an array using PHP.

First method: use array_values ​​function

array_values ​​is one of PHP's built-in functions, which can return a new array in which all key names are reset to A consecutive increasing number starting from 0. This function is great for deleting numeric keys from arrays.

The following is the sample code:

$arr = array(0 => "apple", 1 => "banana", 2 => "orange");
$newArr = array_values($arr); //删除原数组中的数字键名
print_r($newArr);

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)

Second method: Use the unset function

In PHP , the unset function can delete an element in the array or the entire array. If we want to delete the numeric key name, we can use the unset function to achieve this.

The following is a sample code:

$arr = array(0 => "apple", 1 => "banana", 2 => "orange");
unset($arr[0]); //删除数字键名0
unset($arr[1]); //删除数字键名1
unset($arr[2]); //删除数字键名2
print_r($arr);

Output result:

Array
(
)

If you want to delete all numeric key names in the array, you can use the unset function combined with a foreach loop to achieve this .

The following is the sample code:

$arr = array(0 => "apple", 1 => "banana", 2 => "orange");
foreach ($arr as $key => $value) {
    unset($arr[$key]); //删除键名
}
print_r($arr);

Output result:

Array
(
)

The third method: use array_filter function

array_filter is One of PHP's built-in functions, it can filter elements in an array and return a new array. We can use the array_filter function to delete numeric key names in the array.

The following is the sample code:

$arr = array(0 => "apple", 1 => "banana", 2 => "orange");
$newArr = array_filter($arr, function($key) {
    return !is_int($key); //如果键名不是整数,就返回
}, ARRAY_FILTER_USE_KEY);
print_r($newArr);

Output result:

Array
(
)

Summary

Remove numeric keys from array in PHP There are many ways to name, among which the array_values ​​function and the unset function are commonly used. If you want to delete all numeric key names in the array, you can use the unset function combined with a foreach loop to achieve this. In addition, the array_filter function can also filter out numeric key names in the array. These methods can help us perform better operations on PHP arrays.

The above is the detailed content of How to clear numeric key names in 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