Current Location: Home> Latest Articles> array_flip() and array_map() to convert data structures

array_flip() and array_map() to convert data structures

M66 2025-05-14

In PHP, arrays are a very important data structure. For array operations, PHP provides many built-in functions, among which array_flip() and array_map() are two common functions. By combining these two functions, we can efficiently convert the data structure of the array. This article will explain in detail how to use array_flip() and array_map() in combination to achieve this.

What is array_flip() ?

The array_flip() function is a built-in function in PHP, and its function is to exchange keys and values ​​of arrays. This function is very suitable for flipping an array of structures. The function is defined as follows:

 array_flip(array $array): array
  • Parameters : Accepts an array $array .

  • Return value : Returns a new array, where keys and values ​​in the array are swapped.

Example:

 $array = ['apple' => 'fruit', 'carrot' => 'vegetable', 'banana' => 'fruit'];
$flipped = array_flip($array);
print_r($flipped);

Output:

 Array
(
    [fruit] => banana
    [vegetable] => carrot
)

As you can see, array_flip() takes the value of the array as the new key and the original key as the new value.

What is array_map() ?

The array_map() function is used to apply a callback function to each element in the array and return a new array. Its definition is as follows:

 array_map(callable $callback, array $array): array
  • parameter :

    • $callback : The callback function applied to each element of the array.

    • $array : The array to operate.

  • Return value : Returns an array containing the callback function applied.

Example:

 $array = [1, 2, 3, 4, 5];
$squared = array_map(function($item) {
    return $item * $item;
}, $array);

print_r($squared);

Output:

 Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

Use array_flip() and array_map() to implement array structure conversion

The combination of array_flip() and array_map() can help us to convert the structure of arrays into more complex transformations. For example, we might need to convert the value of an array containing key-value pairs into keys and apply some processing functions.

Sample Scenario:

Suppose we have an array containing the product name and its price. We want to swap keys and values ​​and apply a discount to the price (such as 10% off).

Code example:

 $array = [
    'apple' => 100,
    'banana' => 200,
    'cherry' => 300
];

// use array_flip() Take the value as the key,Key as value
$flipped = array_flip($array);

// use array_map() Make a price for each9fold
$discounted = array_map(function($price) {
    return $price * 0.9;
}, $flipped);

print_r($discounted);

Output:

 Array
(
    [100] => 90
    [200] => 180
    [300] => 270
)

In this example, array_flip() first takes the product price as the new key, and then the array_map() function processes 10% off each price, returning a new array.

Application scenarios

  1. Reverse the key-value pair and map it : If you have an array containing key-value pairs, you can first use array_flip() to flip the array, then process the values ​​through array_map() , and finally get a new structure.

  2. Perform batch data processing : for example, batch calculation of discount prices, converting text, modifying the numeric format in the array, etc.

Things to note

  1. Key uniqueness : When using array_flip() , the values ​​in the original array must be unique, otherwise some data will be lost because the same value will be overwritten.

  2. Callback function : When using array_map() , the callback function will be applied to each element of the array to ensure that the logic of the callback function is adapted to your needs.

Hopefully this article helps you understand how to use array_flip() and array_map() to convert the data structure of PHP arrays. If you have any questions, please feel free to ask them!