Current Location: Home> Latest Articles> array_flip() vs array_reverse(): What Are the Differences? How Are These Two Functions Used in PHP?

array_flip() vs array_reverse(): What Are the Differences? How Are These Two Functions Used in PHP?

M66 2025-06-11

In PHP, array_flip() and array_reverse() are two commonly used array functions, each with distinct purposes and usages. Although both functions deal with array operations, their functions and outcomes are completely different. This article will explain in detail the differences between these two functions and how they are applied in real programming scenarios.

1. The array_flip() Function

array_flip() swaps the keys and values of an array. Specifically, it takes the keys of the array as values and the values as keys. Note that the keys after flipping must be unique; otherwise, duplicate values will be lost.

Usage

<?php
$array = array("a" => "apple", "b" => "banana", "c" => "cherry");
$flipped_array = array_flip($array);
print_r($flipped_array);
?>

Result:

Array
(
    [apple] => a
    [banana] => b
    [cherry] => c
)

In this example, array_flip() swaps the keys and values of the array $array. The original keys (like "a", "b", "c") become values, and the original values (like "apple", "banana", "cherry") become keys.

Notes:

  • If the original array contains duplicate values, array_flip() will discard the latter duplicates because array keys must be unique.

  • array_flip() only works with one-dimensional arrays.

2. The array_reverse() Function

array_reverse() reverses the order of an array. Unlike array_flip(), array_reverse() preserves the keys by default, but reverses the order of the array elements.

Usage

<?php
$array = array("a", "b", "c", "d");
$reversed_array = array_reverse($array);
print_r($reversed_array);
?>

Result:

Array
(
    [0] => d
    [1] => c
    [2] => b
    [3] => a
)

In this example, array_reverse() reverses the order of the array $array, changing "a", "b", "c", "d" into "d", "c", "b", "a".

Notes:

  • array_reverse() can optionally preserve the original keys. By default, it resets the keys to zero-based numeric indexes. To keep the original keys, set the second parameter to true.

<?php
$array = array("a" => "apple", "b" => "banana", "c" => "cherry");
$reversed_array = array_reverse($array, true);
print_r($reversed_array);
?>

Result:

Array
(
    [c] => cherry
    [b] => banana
    [a] => apple
)

By setting the second parameter to true, we preserved the array keys.

3. Differences Between array_flip() and array_reverse()

  • Functionality Differences:

    • array_flip() swaps the keys and values of an array, requiring the values to be unique.

    • array_reverse() reverses the order of an array and allows optionally preserving keys.

  • Applicable Scenarios:

    • array_flip() is better suited when you need to use values as new keys.

    • array_reverse() is more appropriate when you want to reverse the order of an array.

  • Return Results:

    • array_flip() returns a new array where original values become keys and original keys become values.

    • array_reverse() returns a new array with elements reversed; keys may be preserved or reset.

4. Conclusion

Although both array_flip() and array_reverse() are common array manipulation functions in PHP, their functionalities and use cases differ entirely. array_flip() is suitable when you need to swap keys and values, while array_reverse() is ideal when you want to reverse the order of an array. Choosing the right function based on your needs can make your code cleaner and more efficient.

I hope this article helps you better understand these two PHP functions and use them confidently in your programming.