Backend Development 5 min read

Using PHP array_flip() to Swap Keys and Values

This article explains the PHP array_flip() function, showing its syntax, behavior with duplicate values, and practical examples that demonstrate how to exchange array keys and values, locate keys by value, and improve code efficiency in backend development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP array_flip() to Swap Keys and Values

PHP is a widely used server‑side scripting language that provides powerful functions for handling arrays. One particularly useful function is array_flip() , which swaps the keys and values of an array.

The basic syntax of array_flip() is:

array array_flip ( array $array )

The function accepts an array and returns a new array with the original keys becoming values and the original values becoming keys. If duplicate values exist, the last key encountered is retained while earlier duplicates are discarded.

Below is a simple example that demonstrates the use of array_flip() :

<?php
$fruits = array(
    "apple"  => "red",
    "orange" => "orange",
    "banana" => "yellow"
);

$flipped_fruits = array_flip($fruits);

print_r($flipped_fruits);
?>

The output of the above code is:

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

In this example, the associative array $fruits contains three elements. After applying array_flip() , the keys and values are exchanged and stored in $flipped_fruits , which is then printed using print_r() .

From the result we can see that the original key "apple" becomes the value "red" in the flipped array, "orange" becomes "orange", and "banana" becomes "yellow".

The array_flip() function is useful in scenarios where you need to find a key by its value. By flipping the array first, you can then use isset() to check for the existence of a specific value.

Example:

<?php
$students = array(
    "Tom"  => 18,
    "John" => 20,
    "Mary" => 19
);

$flipped_students = array_flip($students);

$age_to_find = 20;

if (isset($flipped_students[$age_to_find])) {
    $student_name = $flipped_students[$age_to_find];
    echo "The student with age $age_to_find is $student_name";
} else {
    echo "No student with age $age_to_find";
}
?>

The above code outputs:

The student with age 20 is John

This demonstrates how array_flip() can be combined with isset() to locate a key (student name) based on a value (age).

In summary, the PHP array_flip() function provides a convenient way to exchange array keys and values, handle duplicate values, and simplify tasks such as reverse lookups, making array manipulation more efficient and readable in backend development.

backendPHPphp-functionsarray-manipulationarray_flip
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.