Home  >  Article  >  Backend Development  >  How to reverse key names and key values ​​in an array in PHP

How to reverse key names and key values ​​in an array in PHP

PHPz
PHPzOriginal
2023-07-07 17:33:071712browse

How to reverse the key names and key values ​​in an array in PHP

In PHP, we often need to process arrays. Sometimes, we need to reverse the key names and key values ​​in the array, that is, use the key names as the values ​​of the new array, and use the original key values ​​as the key names of the new array. This article will introduce how to implement this operation in PHP and provide corresponding code examples.

Method 1: Use the array_flip function
PHP provides a built-in function array_flip, which can be used to exchange key names and key values ​​in the array. The following is a code example that uses the array_flip function to reverse array key names and key values:

$originalArray = array("apple" => "red", "banana" => "yellow", "orange" => "orange");

$flippedArray = array_flip($originalArray);

print_r($flippedArray);

In the above code, we define an original array named $originalArray, which contains the name of the fruit as the key name, and the color. as key value. We then use the array_flip function to flip the keys and values ​​in the original array and store the result in a new array called $flippedArray. Finally, we use the print_r function to print out the contents of the $flippedArray array.

Run the above code, you will get the following output:

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

You can see that the key names "apple", "banana" and "orange" in the original array have become new arrays The key values ​​​​of "red", "yellow" and "orange" become the key names of the new array. This completes the reversal of array key names and key values.

Method 2: Use foreach loop
In addition to using the array_flip function, we can also use the foreach loop to reverse the array key name and key value. The following is a code example that uses a foreach loop to reverse array key names and key values:

$originalArray = array("apple" => "red", "banana" => "yellow", "orange" => "orange");

$flippedArray = array();

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

print_r($flippedArray);

In the above code, we first define an empty array $flippedArray to store the reversed result. Then, traverse the original array $originalArray through a foreach loop, use each key name as the key value of the new array, use each key value as the key name of the new array, and store it in $flippedArray. Finally, we use the print_r function to print out the contents of the $flippedArray array.

The output result of the above code is the same as the above code example:

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

Through the above two methods, we can reverse the key name and key value in the array in PHP. Just choose the appropriate method according to actual needs. This can be done easily either using the array_flip function or using a foreach loop.

The above is the detailed content of How to reverse key names and key values ​​in an array 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