Current Location: Home> Latest Articles> PHP Array Key-Value Swap Explained: Using the array_flip Function

PHP Array Key-Value Swap Explained: Using the array_flip Function

gitbox 2025-06-12

What is a PHP Array?

Before we dive into how to swap keys and values in a PHP array, let's first understand the concept of a PHP array. An array in PHP is a data structure that allows you to store multiple values. These values can be of various types, such as integers, strings, or even other arrays. PHP arrays are primarily of two types: indexed arrays and associative arrays. Indexed arrays use numbers as keys, while associative arrays use strings as keys to index the values.

Here are examples of an indexed array and an associative array:


// Indexed array
$num_array = array(1, 2, 3, 4, 5);
// Associative array
$assoc_array = array(
    "name" => "John Doe",
    "age" => 30,
    "email" => "[email protected]"
);

How to Swap Keys and Values in a PHP Array

In PHP, you can easily swap the keys and values in an array using the array_flip() function.

Syntax of the array_flip() Function

The basic syntax of the array_flip() function is as follows:


array_flip(array);

Here, the array parameter specifies the array whose keys and values are to be swapped.

Return Value of array_flip()

The array_flip() function returns a new array, where the original array's keys become the values in the new array, and the original values become the keys.

Example of Using array_flip() to Swap Keys and Values

Here is an example of using the array_flip() function to swap keys and values:


$original_array = array(
    "name" => "John Doe",
    "age" => 30,
    "email" => "[email protected]"
);
$flipped_array = array_flip($original_array);
print_r($flipped_array);

The output of this code will be:


Array
(
    [John Doe] => name
    [30] => age
    [[email protected]] => email
)

As you can see, the keys "name," "age," and "email" from the original array have become the values in the new array, while the original values "John Doe," 30, and "[email protected]" have become the new array's keys.

Things to Keep in Mind

1. The Values in the Original Array Must Be Unique

When using the array_flip() function, if the original array contains duplicate values, only one of the values will be retained in the new array. For example:


$original_array = array(1, 2, 3, 2);
$flipped_array = array_flip($original_array);
print_r($flipped_array);

The output of this code will be:


Array
(
    [1] => 0
    [2] => 3
    [3] => 2
)

As you can see, the value 2 appears twice in the original array. In the new array, only one key is kept for this value, and the other is discarded.

2. The Values in the Original Array Must Be Convertible to Valid Strings

The array_flip() function forces the values in the original array to be converted to strings. If any value cannot be converted to a valid string, a PHP warning will be triggered, and that value will be ignored. For example:


$original_array = array(
    array(1, 2, 3),
    "name" => "John Doe",
    "age" => 30,
    "email" => "[email protected]"
);
$flipped_array = array_flip($original_array);
print_r($flipped_array);

The output will be:


Warning: array_flip(): Can only flip STRING and INTEGER values in /path/to/file.php on line 5

As you can see, because the value array(1, 2, 3) is neither a string nor an integer, it triggers a warning and is ignored.

Conclusion

In this article, we have introduced the basic concepts of PHP arrays and explained how to use the array_flip() function to swap the keys and values of an array. It's important to note that the values in the original array must be unique and convertible to valid strings, otherwise errors or warnings will occur. We hope this article helps you better understand and use PHP arrays to improve your programming efficiency.