Current Location: Home> Latest Articles> Is the array length smaller after using array_flip()? It may be caused by repeated values

Is the array length smaller after using array_flip()? It may be caused by repeated values

M66 2025-06-03

In PHP, array_flip() is a very practical function, and its function is. In other words, the value in the original array will become a new key, and the original key will become the corresponding value.

But you may find that after using array_flip() , the length of the array becomes smaller . Why is this? Is it because there is a duplication in the original value? The answer is: Yes, it is precisely because the value is repeated!

1. Look at a simple example

 $original = [
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'apple',
];

$flipped = array_flip($original);

print_r($flipped);

Output result:

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

As you can see, the $original array originally had three elements, but the $flipped array had only two. Why does 'apple' => c overwrite 'apple' => a ?

2. Internal mechanism of array_flip()

array_flip() requires that the value in the array must be of a type that can be used as a key , that is, a string or an integer. When you flip, if there are two keys corresponding to the same value, the latter overrides the former .

In the above example:

  • 'a' => 'apple''apple' => a

  • 'c' => 'apple''apple' => c (overrides the original 'apple' => a )

Therefore, only 'apple' => c is retained in the flipped array, which causes the array length to decrease.

3. How to avoid this problem?

If you need to preserve all mapping relationships, you can consider mapping values ​​into arrays and manually handle them:

 $original = [
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'apple',
];

$flipped = [];

foreach ($original as $key => $value) {
    if (!isset($flipped[$value])) {
        $flipped[$value] = [];
    }
    $flipped[$value][] = $key;
}

print_r($flipped);

Output:

 Array
(
    [apple] => Array
        (
            [0] => a
            [1] => c
        )

    [banana] => Array
        (
            [0] => b
        )
)

This way you can retain all key information and avoid the problem of being overwritten.

4. Examples of practical application scenarios

Suppose you are doing a URL short-link service (such as https://m66.net/abc123 ), you may have a set of original links and their short codes:

 $links = [
    'abc123' => 'https://m66.net/page1',
    'def456' => 'https://m66.net/page2',
    'ghi789' => 'https://m66.net/page1',
];

$reverse = array_flip($links);
print_r($reverse);

In the result, https://m66.net/page1 will only retain the mapping once (probably 'ghi789' ), and the previous 'abc123' is overwritten. This will have an impact on your short-chain counter-check logic.

So in practical applications, if you want to reversely check short links, you must determine whether there are duplicate values ​​and choose the appropriate data structure.

Summarize

  • array_flip() will interchange the keys and values ​​of the array.

  • If there are duplicate values ​​in the original array, only the last one will be retained after flipping, and the previous one will be overwritten.

  • This situation will cause the array to be smaller .

  • If you need to keep all mapping relationships, you can manually build the array, with the values ​​corresponding to the array.

Understanding this feature will help you avoid data loss when writing PHP programs. I hope this article can help you understand the behavior of array_flip() more clearly!