Home  >  Article  >  Backend Development  >  How to swap array keys and values ​​in php

How to swap array keys and values ​​in php

PHPz
PHPzOriginal
2023-04-18 09:06:381812browse

In PHP, sometimes it is necessary to exchange the keys and values ​​in the array. For example, convert an array with numbers as keys to an associative array with values ​​as keys. Novices may think this is difficult to implement, but in fact, PHP provides a very simple way to achieve this function. In this article, we will learn how to transpose array keys and values ​​using PHP.

Method 1: Use the array_flip function

In PHP, you can use the array_flip function to exchange the keys and values ​​in the array. This function takes an array as input and returns a new array with the original values ​​as keys and the original keys as values. For example:

$numbers = array(1, 2, 3, 4, 5);
$flipped_numbers = array_flip($numbers);

print_r($flipped_numbers);

// Output:
// Array
// (
//     [1] => 0
//     [2] => 1
//     [3] => 2
//     [4] => 3
//     [5] => 4
// )

In the above example, we pass an array with numbers as keys to the array_flip function and save the returned result into the $flipped_numbers variable. This function takes the values ​​of the original array as the keys of the new array, and the keys of the original array as the values ​​of the new array. Therefore, the keys in the $flipped_numbers array are the numbers 1 to 5, and the values ​​are the keys of the original array.

Please note that if there are multiple identical values ​​in the original array, an error will be reported when calling the array_flip function, because the same value cannot be the key of the new array at the same time.

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. This approach requires iterating over the original array and using a new associative array to hold the results. In each loop, use the keys of the original array as the values ​​of the new array, and the values ​​of the original array as the keys of the new array. For example:

$numbers = array(1, 2, 3, 4, 5);
$flipped_numbers = array();

foreach ($numbers as $key => $value) {
    $flipped_numbers[$value] = $key;
}

print_r($flipped_numbers);

// Output:
// Array
// (
//     [1] => 0
//     [2] => 1
//     [3] => 2
//     [4] => 3
//     [5] => 4
// )

In the above example, we first define an empty associative array $flipped_numbers. We then use a foreach loop to iterate over the original array $numbers. In each loop, we save the key of the original array (i.e. the index) into the $value variable and the value of the original array (i.e. the number) into the $key variable. Then, we use $key as the key of the new array $flipped_numbers and $value as the value of the new array. So, the keys in the $flipped_numbers array are the numbers 1 to 5, and the values ​​are the keys of the original array.

Please note that when using a foreach loop, you must ensure that the values ​​in the original array are unique, otherwise the same key will appear. This situation will also occur when using the array_flip function.

Summary

In this article, we learned two ways to swap array keys and values ​​using PHP. The first way is to use the array_flip function which uses the values ​​of the original array as the keys of the new array and the keys of the original array as the values ​​of the new array. The second method is to use a foreach loop, iterate over the original array and swap the keys and values ​​of the original array and save it to a new associative array. Depending on the usage scenario, you can flexibly choose between these two methods. It should be noted that if there are duplicate values ​​in the original array, you cannot use these two methods to swap the keys and values ​​of the original array.

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