PHP array value and key interchange: implementation and performance comparison

WBOY
Release: 2024-05-04 11:12:01
Original
1119 people have browsed it

In PHP, array keys and values ​​can be exchanged through the following methods: array_flip() function: simple syntax, direct key-value exchange, time complexity O(n). Custom function: flexible and can be customized according to needs, but the time complexity is also O(n). Shift operators: Requires PHP knowledge, more efficient in some cases, still O(n) time complexity.

PHP 数组值与键互换:实现与性能比较

PHP array value and key exchange: implementation and performance comparison

In PHP, it is often necessary to exchange the array key and value so that for further processing. There are multiple ways to do this, each with its pros and cons.

Method 1: array\_flip() function

array_flip() The function is a built-in function provided by PHP that is directly used to exchange array values ​​and keys. Using it is very simple, just pass the original array as a parameter:

$originalArray = [
    'name' => 'John Doe',
    'age' => 30,
];

$flippedArray = array_flip($originalArray);

print_r($flippedArray);
Copy after login

Output:

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

Method 2: Custom function

You can also define a custom one function to interchange array values ​​and keys:

function flipArray($array) {
    $flippedArray = [];
    foreach ($array as $key => $value) {
        $flippedArray[$value] = $key;
    }
    return $flippedArray;
}
Copy after login

This custom function works similarly to the array_flip() function, but it provides greater flexibility. For example, you can add additional logic to handle special cases or modify the output format as needed.

Method 3: Displacement operator

The displacement operator (=>) can also be used to interchange array keys and values. This approach requires a certain level of PHP knowledge, but it may be more efficient than others in some cases:

$originalArray = [
    'name' => 'John Doe',
    'age' => 30,
];

$flippedArray = [];
foreach ($originalArray as $key => $value) {
    $flippedArray[$value] = $key;
}
Copy after login

Practical Case

Suppose there is a An array of product information, whose price needs to be obtained based on the product name. Arrays can be quickly transformed using value and key swapping to easily find the price data you need:

$products = [
    'Apple' => 10,
    'Orange' => 5,
];

// 使用 array_flip() 互换键和值
$flippedProducts = array_flip($products);

// 根据产品名称获取价格
$price = $flippedProducts['Orange'];
Copy after login

In the above example, $price will now contain the product Orange price without traversing the entire original array to find it.

Performance comparison

Here is a quick comparison of performance when using different methods to swap arrays:

O(n)Custom functionO(n)Shift operatorO(n)
Method Time complexity
##array_flip()
It should be noted that the performance complexity is the same for all methods because they all need to traverse the entire array. Therefore, which method to choose depends on the specific use case and the developer's personal preference.

The above is the detailed content of PHP array value and key interchange: implementation and performance comparison. 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