Backend Development 6 min read

Using PHP’s array_flip() Function to Swap Keys and Values

This article explains PHP’s array_flip() function, detailing its syntax, behavior with duplicate values, and practical usage through examples that demonstrate swapping array keys and values, locating keys by value, and improving code efficiency in backend development.

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

PHP is a widely used server‑side scripting language that provides many powerful functions for handling arrays and data. One particularly useful function is array_flip() , which swaps the keys and values of an array. This article introduces how to use array_flip() and demonstrates its behavior with code examples.

The basic syntax of array_flip() is:

array array_flip ( array $array )

The function accepts an array as its argument and returns a new array with the original keys becoming values and the original values becoming keys. If the original array contains duplicate values, array_flip() keeps the last key and discards the earlier ones.

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

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

$flipped_fruits = array_flip($fruits);

print_r($flipped_fruits);
?>
Array
(
    [red] => apple
    [orange] => orange
    [yellow] => banana
)

In this example, the associative array $fruits contains three elements. After calling array_flip() , the keys and values are exchanged and stored in $flipped_fruits , which is then printed with print_r() . The output shows that the original keys "apple", "orange", and "banana" become the values "red", "orange", and "yellow" respectively.

The array_flip() function has many practical scenarios. For instance, when you need to find a key by a given value, you can first flip the array and then use isset() to check whether the flipped array contains that value.

<?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 example defines an associative array $students mapping names to ages, flips it, and then looks up the name corresponding to the age 20, demonstrating how array_flip() can be used to reverse look‑ups.

These examples show the powerful capabilities of array_flip() in PHP, such as locating corresponding keys, removing duplicate values, and simplifying code.

In summary, the article covered the usage of the PHP function array_flip() , its syntax, basic usage, practical scenarios, and tips for improving array handling efficiency in backend development.

Java learning material download

C language learning material download

Frontend learning material download

C++ learning material download

PHP learning material download

backendPHPCode Exampledata-manipulationphp-arrayarray_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.