Backend Development 5 min read

Understanding PHP's array_flip() Function: Syntax, Parameters, Return Values, and Examples

This article explains PHP's array_flip() function, which swaps array keys and values, detailing its syntax, parameters, return behavior, and providing three practical code examples with output to illustrate correct usage and common pitfalls.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding PHP's array_flip() Function: Syntax, Parameters, Return Values, and Examples

In PHP programming, arrays are a commonly used data structure, and the array_flip() function is a very practical built‑in function. This article introduces the usage of array_flip() and provides concrete code examples.

The array_flip() function swaps the keys and values of an array: it takes an array as a parameter and returns a new array where the original keys become values and the original values become keys.

array_flip() Function Syntax

<code>array array_flip ( array $array )</code>

Parameter Description

array : The array whose keys and values are to be swapped.

Return Value

The function returns a new array with exchanged keys and values. If any value in the original array is not a string or integer, a warning is generated.

Usage of array_flip()

Example 1

<code>$array = array("a" => 1, "b" => 2, "c" => 3);
$flippedArray = array_flip($array);

print_r($flippedArray);</code>

Output:

<code>Array
(
    [1] => a
    [2] => b
    [3] => c
)</code>

In this example, an associative array $array is defined, then array_flip() swaps its keys and values. The resulting array shows the original values (1, 2, 3) as keys and the original keys ("a", "b", "c") as values.

Example 2

<code>$array = array("apple" => "red", "banana" => "yellow", "orange" => "orange", "grape" => "purple");
$flippedArray = array_flip($array);

print_r($flippedArray);</code>

Output:

<code>Array
(
    [red] => apple
    [yellow] => banana
    [orange] => orange
    [purple] => grape
)</code>

Here the keys represent fruit names and the values represent colors. After applying array_flip() , the colors become keys and the fruit names become values.

Example 3

<code>$array = array(1 => "a", 2 => "b", 3 => "c", 4 => "a");
$flippedArray = array_flip($array);

print_r($flippedArray);</code>

Output:

<code>Array
(
    [a] => 4
    [b] => 2
    [c] => 3
)</code>

This example uses a numerically indexed array. After flipping, the values become keys, and because the value "a" appears twice, the resulting array keeps the last corresponding index (4) as the key for "a".

The array_flip() function is a handy tool for exchanging array keys and values, but it requires that both keys and values be strings or integers; otherwise, it will trigger an error.

Recommended PHP Learning Resources

Vue3+Laravel8+Uniapp Beginner to Advanced Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Expert Recommended Course

Workerman+TP6 Real‑time Chat System – Limited Time Offer

BackendPHPArrayTutorialphp-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

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.