Current Location: Home> Latest Articles> Use array_combine to implement key-value flip

Use array_combine to implement key-value flip

M66 2025-05-12

How to use the array_combine function in PHP to implement array key-value flip?

In PHP, the array_combine() function can be used to combine two arrays into an associative array. This function accepts two parameters, one is the keys of the array and the other is the values ​​of the array. But do you know? array_combine() can also be used to implement key-value flips of arrays, although this is not the case for its common purpose.

1. Basic usage of array_combine() function

The syntax of the array_combine() function is as follows:

 array_combine(array $keys, array $values) : array
  • $keys : an array used to create a new array key.

  • $values : an array used to create a new array value.

This function returns an associative array composed of two arrays.

2. How to use array_combine() to flip key value?

Flip the keys and values ​​of the array, in fact, take the "value" of the original array as the "key" of the new array, and the original "key" as the "value" of the new array. We can do this by extracting the "key" and "value" of the original array and using array_combine() .

Suppose we have an array like this:

 $array = [
    'apple' => 1,
    'banana' => 2,
    'orange' => 3
];

We want to flip the key value of the array and get the following result:

 $flippedArray = [
    1 => 'apple',
    2 => 'banana',
    3 => 'orange'
];

Next, let’s see how to implement this function through PHP’s array_combine() function.

3. Code examples to implement key-value flip

 <?php
$array = [
    'apple' => 1,
    'banana' => 2,
    'orange' => 3
];

// Get the keys and values ​​of the array
$keys = array_keys($array);
$values = array_values($array);

// use array_combine() Function flips keys and values
$flippedArray = array_combine($values, $keys);

// Output flipped array
print_r($flippedArray);
?>

4. Code analysis

  1. Get keys and values:

    • Use array_keys($array) to get all keys in the original array.

    • Use array_values($array) to get all values ​​in the original array.

  2. Flip the key value:

    • Use the array_combine() function to use the "value" of the original array as the "key" of the new array, and use the "key" of the original array as the "value" of the new array.

  3. Output result:

    • Use print_r($flippedArray) to output the flipped array.

5. Things to note

  • When using array_combine() , the lengths of the two arrays must be equal. If the lengths of the key and value arrays are not equal, PHP returns a false .

  • If there are duplicate values ​​in the original array, the flipped array will lose the duplicate values ​​because the keys of the array are unique.

6. Summary

Through PHP's array_combine() function, we can easily implement key-value flip of arrays. You only need to extract the keys and values ​​of the original array, and then combine them into a new array through the array_combine() function. This method is concise and efficient, and is suitable for scenarios where array key values ​​need to be reversed.

Hope this article helps you understand how to use the array_combine() function in PHP to implement array key value flip. If you have any questions or further requirements, please feel free to contact me.