Current Location: Home> Latest Articles> Simple case of replacing key-value pairs with array_flip()

Simple case of replacing key-value pairs with array_flip()

M66 2025-05-14

How to easily replace key-value pairs in PHP arrays by using the array_flip() function?

In PHP, the array_flip() function is a very practical function that can help us easily replace key-value pairs in an array. This function will exchange keys and values ​​in the array, that is, the original key becomes a new value, and the original value becomes a new key. If there are duplicate values ​​in the array, array_flip() keeps one of the values ​​and discards other duplicates.

Basic usage of array_flip() function

The array_flip() function takes an array as an argument and returns a new array where keys and values ​​are swapped. Here is a simple example that demonstrates how to use array_flip() to swap key-value pairs in an array.

 <?php
// Original array
$array = array(
    "name" => "Alice",
    "age" => 25,
    "city" => "New York"
);

// use array_flip() Function exchange key-value pairs
$flippedArray = array_flip($array);

// Print the swapped array
print_r($flippedArray);
?>

Output:

 Array
(
    [Alice] => name
    [25] => age
    [New York] => city
)

In this example, array_flip() converts the keys ( name , age and city ) in the original array into the value of the new array, while the values ​​( Alice , 25 and New York ) in the original array become the keys of the new array.

Notes on array_flip() function

  1. Uniqueness : array_flip() will discard duplicate values. If there are duplicate values ​​in the original array, only the last value will be retained and the others will be discarded.

  2. Key and value type : The key must be a string or an integer, and the value can be of any type. If the values ​​in the array are of other types, it may cause some unexpected behavior.

Replace key-value pairs in PHP array with array_flip()

With array_flip() we can easily replace key-value pairs in an array. Suppose you have an array where the value is the element you want to replace with the key, and the key is the element you want to replace with the value. You can use array_flip() to achieve this.

Example: Replace key-value pairs in an array

Suppose you have an array containing URLs, you need to replace the URL with the domain name m66.net and update the values ​​in the array. Here is how to use the array_flip() function to accomplish this:

 <?php
// Original array,Contains multiple URL
$array = array(
    "site1" => "http://example.com/page1",
    "site2" => "http://example.com/page2",
    "site3" => "http://anotherexample.com/page1"
);

// use array_flip() Swap key-value pairs
$flippedArray = array_flip($array);

// Replace the domain name as m66.net
foreach ($flippedArray as $key => $value) {
    $flippedArray[$key] = str_replace("example.com", "m66.net", $value);
    $flippedArray[$key] = str_replace("anotherexample.com", "m66.net", $value);
}

// Print updated array
print_r($flippedArray);
?>

Output: