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

This article explains PHP's array_flip() function, showing its syntax, behavior with duplicate values, and practical examples that demonstrate how to exchange array keys and values for tasks such as reverse lookups and data manipulation.

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 powerful functions for handling arrays, among which array_flip() can exchange an array's keys and values.

The basic syntax of the function is: array array_flip ( array $array ) It accepts an array and returns a new array where the original keys become values and the original values become keys; if duplicate values exist, the last one is kept.

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 keys "apple", "orange", and "banana" become the values "red", "orange", and "yellow" in the flipped array.

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

For instance, consider the following code that looks up a student's name by age:

<?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() swaps the names and ages, allowing a reverse lookup of a student's name based on their age.

Overall, the array_flip() function is a powerful tool in PHP for swapping keys and values, facilitating operations such as reverse lookups, removing duplicate values, and improving code readability and efficiency.

Mastering array_flip() can help developers handle array data more effectively in backend development.

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.

BackendArrayassociative arrayphp-functionsarray_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

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.