PHP array key value reversal: exploration of efficient solutions

王林
Release: 2024-05-01 15:00:03
Original
1011 people have browsed it

Array key-value reversal can be achieved in PHP through a variety of efficient methods: use the array_flip() function to directly exchange keys and values. Write a custom function to combine values ​​and keys into a new array using array_combine(). Use the mapping functions array_map() and array_column() to convert array structures and recombine them. Performance comparison shows that the array_flip() function is fastest in small arrays, while custom functions and mapping functions have advantages when the array size is larger.

PHP 数组键值颠倒:高效方案探究

PHP array key value reversal: efficient solution exploration

Array key value reversal, that is, exchanging the keys and values ​​in the array, This is a common operation in PHP. This article will explore several efficient solutions and demonstrate them through practical cases.

Method 1: array_flip() function

$array = ['name' => 'John Doe', 'age' => 30];
$flipped_array = array_flip($array);
print_r($flipped_array);
Copy after login

Output:

Array
(
    [John Doe] => name
    [30] => age
)
Copy after login
Copy after login
Copy after login

Method 2: Custom function

function flip_array($array) {
    return array_combine(array_values($array), array_keys($array));
}

$array = ['name' => 'John Doe', 'age' => 30];
$flipped_array = flip_array($array);
print_r($flipped_array);
Copy after login

Output:

Array
(
    [John Doe] => name
    [30] => age
)
Copy after login
Copy after login
Copy after login

Method 3: Mapping function

$array = ['name' => 'John Doe', 'age' => 30];
$flipped_array = array_map(function($key, $value) { return [$value, $key]; }, array_keys($array), array_values($array));
$flipped_array = array_combine(array_column($flipped_array, 0), array_column($flipped_array, 1));
print_r($flipped_array);
Copy after login

Output:

Array
(
    [John Doe] => name
    [30] => age
)
Copy after login
Copy after login
Copy after login

Performance comparison

When the array size is small, the array_flip() function is the fastest. For larger arrays, custom functions and mapping functions have better performance.

Practical case

Array key value reversal can be used in various scenarios, for example:

  • From user ID to user name Mapping
  • Mapping from country code to country name
  • Create an inverted index to increase search speed

The above is the detailed content of PHP array key value reversal: exploration of efficient solutions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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