How to remove the key of a two-dimensional array in PHP

PHPz
Release: 2023-04-24 16:51:29
Original
702 people have browsed it

In PHP programming, we often use arrays to store some data, and PHP provides many convenient array operation functions and methods. However, when we need to process the keys of a two-dimensional array, sometimes we need to remove the keys of the two-dimensional array and only retain the values. What should we do in this case? This article will introduce you how to use PHP to remove keys from a two-dimensional array.

1. What is a two-dimensional array?

A two-dimensional array is a data type in which the elements in the array are also arrays. It consists of a main array and several sub-arrays, and each sub-array can be regarded as an independent array. We can use subscripts to access elements in a two-dimensional array. For example, $arr0 means accessing the element in the first row and first column of a two-dimensional array.

The following is a simple two-dimensional array example:

$arr = array(
    array('name' => 'Tom', 'age' => 18),
    array('name' => 'Jerry', 'age' => 20),
);
Copy after login

2. The keys and values ​​of the two-dimensional array

In the above example, the keys of the two-dimensional array are respectively are 0 and 1, the keys of each sub-array are name and age respectively, and the corresponding values ​​are Tom, 18, Jerry and 20 respectively. In actual development, sometimes we need to remove the keys of the two-dimensional array and keep only the values. In this case, we can use the array_values ​​function provided by PHP to achieve this.

The array_values ​​function is to return all the values ​​in a two-dimensional array and re-establish an index array. That is, it discards the keys of the original array and only retains the values. The following is an example of using the array_values ​​function:

$newArr = array_values($arr);
print_r($newArr);
Copy after login

The output result is as follows:

Array
(
    [0] => Array
        (
            [name] => Tom
            [age] => 18
        )

    [1] => Array
        (
            [name] => Jerry
            [age] => 20
        )
)
Copy after login

As you can see, the $newArr array only retains the values ​​​​in the original array, and each sub-array The keys have been discarded, leaving only the values ​​corresponding to name and age.

3. array_map function

The array_values ​​function introduced above can only operate on each sub-array in the two-dimensional array. If we need to operate on the entire two-dimensional array, we can use PHP The provided array_map function.

The array_map function can apply a custom function to all elements in a two-dimensional array and then return a new two-dimensional array whose keys are reset to integer indexes starting from 0. The following is an example of using the array_map function:

function removeKey($arr) {
    return array_values($arr);
}
$newArr = array_map('removeKey', $arr);
print_r($newArr);
Copy after login

The output is as follows:

Array
(
    [0] => Array
        (
            [0] => Tom
            [1] => 18
        )

    [1] => Array
        (
            [0] => Jerry
            [1] => 20
        )
)
Copy after login

In this example, we define a custom function named removeKey, which is similar to array_values. It's just that it can be used by the array_map function. We pass removeKey as the callback function of array_map, and $arr is the two-dimensional array that needs to be processed. Finally, $newArr is the new array after processing.

As you can see, the $newArr array only retains the values ​​in the original array, and the keys of each sub-array are discarded, leaving only the values ​​corresponding to name and age.

Summary:

In PHP development, we often use arrays, and array operations are very flexible, which can be said to be the foundation of PHP programming. When we need to remove the keys from a two-dimensional array, we can use the array_values ​​function or array_map function provided by PHP. These functions can discard the keys of the original array and retain only the values, allowing us to process data in two-dimensional arrays more conveniently.

The above is the detailed content of How to remove the key of a two-dimensional array in PHP. 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