Current Location: Home> Latest Articles> How to simulate value duplication to test the override of array_flip()

How to simulate value duplication to test the override of array_flip()

M66 2025-06-03

array_flip() is a function in PHP, and its function is to swap keys and values ​​of arrays. That is, the value in the array will become a key, and the original key will become a value. However, this function may experience coverage problems in some cases, especially when there are duplicate values ​​in the array. In this case, the later keys overwrite the previous key. This article will test the override of array_flip() function by simulating the case of repeated values.

Basic use of array_flip() function

First, let's briefly review the usage of the array_flip() function. The function takes an array as an argument and returns a new array where the keys and values ​​of the array are interchangeable. If the value of the array is unique, then each key will be preserved.

 <?php
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$flipped = array_flip($array);
print_r($flipped);
?>

The output will be:

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

The case of duplication of values

The problem occurs when the array contains duplicate values. If we pass a key with the same value to array_flip() , the subsequent key overwrites the previous key. Let's look at an example:

 <?php
$array = ['a' => 1, 'b' => 2, 'c' => 2];
$flipped = array_flip($array);
print_r($flipped);
?>

The output result will be:

 Array
(
    [1] => a
    [2] => c
)

As you can see, both keys b and c have the same value of 2 , but after array_flip() is executed, key c replaces key b because it has the same value. array_flip() only retains the last key that appears. This override issue is usually something we need to pay attention to when using array_flip() .

How to simulate value duplication to test coverage issues

To better understand this problem, some test cases can be created, simulated duplicate values ​​in different scenarios, and observed the behavior of array_flip() . Here are some common test scenarios.

Scenario 1: There are multiple identical values ​​in an array

 <?php
$array = ['a' => 1, 'b' => 1, 'c' => 2, 'd' => 2];
$flipped = array_flip($array);
print_r($flipped);
?>

The output will be:

 Array
(
    [1] => b
    [2] => d
)

In this example, the values ​​1 and 2 appear twice each, and the last key ( b and d ) will override the previous key.

Scenario 2: The values ​​of the array are exactly the same

 <?php
$array = ['a' => 1, 'b' => 1, 'c' => 1];
$flipped = array_flip($array);
print_r($flipped);
?>

The output will be:

 Array
(
    [1] => c
)

In this case, all keys have values ​​of 1 , so only the last key c will be retained.

Scenario 3: More complex array structure

 <?php
$array = ['a' => 1, 'b' => 2, 'c' => 2, 'd' => 3, 'e' => 1];
$flipped = array_flip($array);
print_r($flipped);
?>

The output will be:

 Array
(
    [1] => e
    [2] => c
    [3] => d
)

As you can see here, the values ​​1 and 2 appear twice each, and e and c overwrite the previous keys. 3 is the only value that occurs, so d is preserved.

Summarize

By simulating different repeat values, we can see the behavior of the array_flip() function when processing repeated values. The function overrides the previous key, retaining the last occurrence of the key. This override behavior can lead to unexpected results, so we need to be extra careful when using array_flip() , especially when there are duplicate values ​​in the array.