Current Location: Home> Latest Articles> Use array_flip() to optimize the data format of front-end drop-down list

Use array_flip() to optimize the data format of front-end drop-down list

M66 2025-06-03

In daily PHP development, we often need to build drop-down lists (select options) for the front-end. These lists are usually derived from databases or configuration files, and their original structure may not be suitable for direct use. In order to process this data more conveniently and improve efficiency, PHP provides a very practical function - array_flip() .

This article will introduce how to use array_flip() to quickly optimize the data format of the drop-down list, simplify the code, and improve the data collaboration efficiency of the front and back ends.

1. What is array_flip()

array_flip() is a built-in array function in PHP, and its function is to exchange keys and values ​​in an array . For example:

 $original = [
    'apple' => 'apple',
    'banana' => 'banana',
];

$flipped = array_flip($original);

// Output result:['apple' => 'apple', 'banana' => 'banana']

This function is particularly suitable for adjusting the array structure in certain scenarios, such as inverting the key value for easy front-end display or back-end data mapping.

2. Common data structures for drop-down lists

Taking the country list as an example, the background data may be in the following form:

 $countries = [
    'CN' => 'China',
    'US' => 'USA',
    'JP' => 'Japan',
];

The format usually required by the front-end may be an array like this:

 [
    { "label": "China", "value": "CN" },
    { "label": "USA", "value": "US" },
    { "label": "Japan", "value": "JP" }
]

This format is often used in Vue, React, or Element UI.

3. How to use array_flip() to convert structure?

If the array you are holding is as follows:

 $countryNames = [
    'China' => 'CN',
    'USA' => 'US',
    'Japan' => 'JP',
];

You can directly use array_flip() to flip it into:

 $flipped = array_flip($countryNames);
// ['CN' => 'China', 'US' => 'USA', 'JP' => 'Japan']

Next, it can be easily converted to front-end available formats:

 $options = [];

foreach ($flipped as $value => $label) {
    $options[] = [
        'label' => $label,
        'value' => $value
    ];
}

header('Content-Type: application/json');
echo json_encode($options);

Output result:

 [
    { "label": "China", "value": "CN" },
    { "label": "USA", "value": "US" },
    { "label": "Japan", "value": "JP" }
]

In this way, the data that originally needed to manually adjust the structure became simple and efficient with the help of a line array_flip() .

4. Use with the backend interface

Suppose we need to provide a list of countries to the front end through an API, and the interface code is as follows:

 <?php
// api/countries.php

$countryList = [
    'China' => 'CN',
    'USA' => 'US',
    'Japan' => 'JP',
];

$flipped = array_flip($countryList);

$result = [];

foreach ($flipped as $value => $label) {
    $result[] = [
        'label' => $label,
        'value' => $value,
    ];
}

// Set cross-domain header(Example)
header('Access-Control-Allow-Origin: https://m66.net');
header('Content-Type: application/json');
echo json_encode($result);

The front-end only needs to request the interface:

 GET https://m66.net/api/countries.php

Get the drop-down option data in standard format.

5. Summary

Using array_flip() can greatly simplify the conversion process of data structures, and is especially suitable for the backend to provide data in a unified format to the frontend, such as the pull-down options. Compared to manually constructing data structures, array_flip() method is not only efficient and clear, but also reduces the probability of errors.

During development, flexibly mastering similar PHP native functions can improve the efficiency and quality of the overall project in detail.