Home > Backend Development > PHP Problem > How to remove specified key value from php two-dimensional array

How to remove specified key value from php two-dimensional array

PHPz
Release: 2023-04-20 11:44:27
Original
775 people have browsed it

In PHP, a two-dimensional array is a very common data structure. It can store a large amount of data and can be sorted, searched and other operations according to certain rules. However, in some cases, we may need to remove the key values ​​​​from the two-dimensional array to facilitate operation.

At this time, we can use some PHP functions to achieve this goal. Below, I will introduce you to several commonly used methods:

  1. array_values() function

array_values() function is to return all the values ​​​​of an array, and The array will be reindexed. We can use this feature to remove the key values ​​​​from the two-dimensional array.

Sample code:

// 定义一个二维数组
$arr = [
    ['name' => 'Alice', 'age' => 20],
    ['name' => 'Bob', 'age' => 22],
    ['name' => 'Mike', 'age' => 25],
];

// 使用array_values()函数
$newArr = array_values($arr);

// 输出新的数组
print_r($newArr);
Copy after login

Running results:

Array
(
    [0] => Array
        (
            [name] => Alice
            [age] => 20
        )

    [1] => Array
        (
            [name] => Bob
            [age] => 22
        )

    [2] => Array
        (
            [name] => Mike
            [age] => 25
        )

)
Copy after login

As you can see, through the array_values() function, we successfully removed the key values ​​​​of the two-dimensional array, and New indexes are also regenerated in sequence.

  1. foreach loop

In addition to using functions, we can also remove the key values ​​​​of the two-dimensional array through loops. The specific method is to use a foreach loop to traverse the array and store the value of each element in a new array, as shown below:

// 定义一个新的数组
$newArr = [];

// 使用foreach循环
foreach ($arr as $item) {
    $newArr[] = array_values($item);
}

// 输出新的数组
print_r($newArr);
Copy after login

The running result is the same as the previous example.

  1. array_map() function

array_map() function is one of the PHP built-in functions, which is used to apply a callback function to each element in the array . In this article, we can use the array_map() function to store values ​​as array elements into a new array, thus removing the keys from the original array.

Sample code:

// 使用array_map()函数
$newArr = array_map('array_values', $arr);

// 输出新的数组
print_r($newArr);
Copy after login

The running result is the same as the previous example.

Summary

Through the above three methods, we can easily remove the key values ​​​​of the PHP two-dimensional array and generate a new array. In this way, in practical applications, we can operate arrays more flexibly and solve problems quickly.

The above is the detailed content of How to remove specified key value from php two-dimensional 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