Home  >  Article  >  Backend Development  >  How to swap keys and values ​​in PHP arrays

How to swap keys and values ​​in PHP arrays

WBOY
WBOYOriginal
2023-09-05 16:34:571845browse

PHP 数组如何将键和值互换

How PHP arrays exchange keys and values

In PHP, arrays are an important and commonly used data structure. Sometimes we need to interchange the keys and values ​​of the array to meet certain needs. This article will introduce several methods to interchange the keys and values ​​of PHP arrays, and provide corresponding code examples.

Method 1: Use the array_flip function

The array_flip function is a built-in function in PHP, which can be used to exchange the keys and values ​​of the array. The following is a code example that uses the array_flip function to interchange the keys and values ​​of the array:

$fruit = array(
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange'
);

$color = array_flip($fruit);

print_r($color);

The output result is:

Array
(
    [red] => apple
    [yellow] => banana
    [orange] => orange
)

As can be seen from the above example, using the array_flip function can be very simple Swap the keys and values ​​of the array.

Method 2: Use foreach loop

In addition to using the array_flip function, we can also use foreach loop to exchange the keys and values ​​of the array. The following is a code example that uses a foreach loop to swap the keys and values ​​of an array:

$fruit = array(
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange'
);

$color = array();

foreach ($fruit as $key => $value) {
    $color[$value] = $key;
}

print_r($color);

The output is:

Array
(
    [red] => apple
    [yellow] => banana
    [orange] => orange
)

By looping through the array and swapping the keys and values, we can get and Same result using array_flip function.

Method 3: Use array_walk function

The array_walk function is an array traversal function provided by PHP. We can modify it while traversing the array. The following is a code example that uses the array_walk function to interchange the keys and values ​​of an array:

$fruit = array(
    'apple' => 'red',
    'banana' => 'yellow',
    'orange' => 'orange'
);

$color = array();

array_walk($fruit, function ($value, $key) use (&$color) {
    $color[$value] = $key;
});

print_r($color);

The output is:

Array
(
    [red] => apple
    [yellow] => banana
    [orange] => orange
)

By passing an anonymous function to the array_walk function, we can traverse the array while Modify it so that the keys and values ​​of the array are interchanged.

Summary:

This article introduces three common methods to interchange the keys and values ​​of PHP arrays, namely using the array_flip function, using the foreach loop and using the array_walk function. Choosing the appropriate method according to the actual situation can help us realize the key-value exchange of the array and meet the corresponding needs. Hope this article is helpful to you!

The above is the detailed content of How to swap keys and values ​​in PHP arrays. 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