Home  >  Article  >  Backend Development  >  How to transpose a two-dimensional array of key names in PHP

How to transpose a two-dimensional array of key names in PHP

PHPz
PHPzOriginal
2023-04-23 10:10:34548browse

In PHP programming, we often use arrays, and there are many operations on arrays. One of the more interesting operations is the transposition of key names in a two-dimensional array.

The so-called two-dimensional array means that each element in an array is an array. Key name transposition refers to changing the key name of each element in the two-dimensional array to its array subscript, and at the same time transposing the array corresponding to each element in the two-dimensional array.

This article will explain how to use PHP to implement the key name transposition operation for a two-dimensional array.

1. Create a two-dimensional array

Before starting to explain, you need to create a two-dimensional array first. We can use the following code:

$originArr = array(
    'fruit1' => array('name' => 'apple', 'color' => 'red'),
    'fruit2' => array('name' => 'banana', 'color' => 'yellow'),
    'fruit3' => array('name' => 'orange', 'color' => 'orange')
);

In this two-dimensional array, each element is an associative array, which contains the name and color of the fruit. Now, we have to transpose the key name on it.

2. Use the array_column function to transpose a two-dimensional array

PHP provides a function called array_column, which can be used to extract a certain column from a multi-dimensional array. In this article, we are going to use this function to implement key name transposition.

First, we need to extract all the key names in the two-dimensional array and convert it into a one-dimensional array. Use the array_keys function to extract all key names, and use the array_values ​​function to extract all values. We only need to pass the array returned by the array_keys function as the second parameter to the array_column function.

The code is implemented as follows:

$keys = array_keys($originArr);
$newArr = array_column($originArr, null, $keys);

In this step, we use the key name of each element in the two-dimensional array as the subscript of the new array. The array corresponding to each element in the original array becomes the value of the new array.

3. Transpose the key names in the sub-array

In the current new array, although the key names have been transposed, each value in the new array is also an array, and The key names in each array are not the subscripts we need. Therefore, we also need to transpose the key names of each sub-array in the new array.

We can use the array_map function to apply a callback function to each sub-array in the newArr array to transpose the key names. The code is implemented as follows:

$newArr = array_map(function($arr){ 
    return array_combine(array_keys($arr), $arr);
}, $newArr);

Here, we first use the array_keys function to extract the current key name of the array, and then use the array_combine function to form a new associative array using the extracted key name and the current value. In this way, we have successfully transposed the key names of each sub-array in the new array.

4. Complete code implementation

Through the above steps, for a given two-dimensional array, we can implement the key name transposition operation. The following is the complete code implementation:

$originArr = array(
    'fruit1' => array('name' => 'apple', 'color' => 'red'),
    'fruit2' => array('name' => 'banana', 'color' => 'yellow'),
    'fruit3' => array('name' => 'orange', 'color' => 'orange')
);

$keys = array_keys($originArr);
$newArr = array_column($originArr, null, $keys);

$newArr = array_map(function($arr){ 
    return array_combine(array_keys($arr), $arr);
}, $newArr);

print_r($newArr);

The output results are as follows:

Array
(
    [name] => Array
        (
            [fruit1] => apple
            [fruit2] => banana
            [fruit3] => orange
        )

    [color] => Array
        (
            [fruit1] => red
            [fruit2] => yellow
            [fruit3] => orange
        )

)

As you can see, we have successfully transposed the key name of each element in the two-dimensional array into a new array subscript, and each key name in the subarray is also transposed to a new subscript.

Summary

In this article, we describe the key name transposition operation for two-dimensional arrays in PHP, and introduce how to use the array_column and array_map functions to implement this operation. When we need to operate on multi-dimensional arrays, this operation can save the amount of code and improve development efficiency.

The above is the detailed content of How to transpose a two-dimensional array of key names 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