Master PHP’s array_flip(): Swap Keys and Values with Real Code Examples

Learn how PHP’s array_flip() function swaps array keys and values, understand its syntax, handle duplicate values, and see practical examples—including reversing fruit arrays and locating students by age—demonstrating how to simplify data lookups and improve code efficiency.

php Courses
php Courses
php Courses
Master PHP’s array_flip(): Swap Keys and Values with Real Code Examples

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

Syntax

array array_flip ( array $array )

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

Basic Example

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

The output is:

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

This shows that the original keys ("apple", "orange", "banana") become the values, and the original values become the keys.

Practical Use Case: Finding a Key by Value

When you need to locate a key based on a value, you can first flip the array and then use isset() to check for the desired 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 script outputs: The student with age 20 is John In this example, the associative array $students is flipped so that ages become keys, allowing a quick lookup of the corresponding student name.

Conclusion

The array_flip() function is a powerful tool in PHP for exchanging array keys and values, simplifying tasks such as reverse lookups and removing duplicate values, thereby improving code readability and efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPdata manipulationarray_flipphp arrays
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

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.