Current Location: Home> Latest Articles> Use array_flip() to reverse the value and key and then process the statistics

Use array_flip() to reverse the value and key and then process the statistics

M66 2025-06-07

How to reverse the array key value using array_flip() , then combine array_count_values() statistics and process array data?

In PHP, array_flip() and array_count_values() are two commonly used functions. array_flip() is used to swap keys and values ​​of an array, while array_count_values() is used to count the occurrences of all values ​​in the array. By combining these two functions, array data can be easily processed and more complex data operations can be achieved.

1. array_flip() function

The array_flip() function can swap keys and values ​​of an array. That is, the keys in the array become values, and the value becomes keys. It should be noted that the values ​​in the array must be unique, otherwise data will be lost.

 $array = [
    'apple' => 5,
    'banana' => 3,
    'orange' => 4,
];

$flippedArray = array_flip($array);
print_r($flippedArray);

Output:

 Array
(
    [5] => apple
    [3] => banana
    [4] => orange
)

In this example, the array_flip() function interchanges the key values ​​in the original array to obtain a new array where the original value becomes a new key.

2. array_count_values() function

The array_count_values() function counts the number of occurrences of each value in the array. It returns an associative array where the keys are values ​​in the array, and the values ​​are the number of times these values ​​appear in the array.

 $array = ['apple', 'banana', 'orange', 'apple', 'banana', 'apple'];

$countedArray = array_count_values($array);
print_r($countedArray);

Output:

 Array
(
    [apple] => 3
    [banana] => 2
    [orange] => 1
)

In this example, array_count_values() counts the number of occurrences of each fruit in the array and returns the result as a new array.

3. Use array_flip() and array_count_values() in combination

Suppose you have an array containing the product name and its price. You want to reverse the array by array_flip() and then count the number of occurrences of each price. You can follow the following steps:

 $array = [
    'apple' => 5,
    'banana' => 3,
    'orange' => 5,
    'grape' => 3,
    'peach' => 4,
];

$flippedArray = array_flip($array);
print_r($flippedArray);

// Statistics the number of times the price appears
$countedArray = array_count_values($flippedArray);
print_r($countedArray);

Output:

 Flipped Array:
Array
(
    [5] => apple
    [3] => banana
    [4] => peach
)

Counted Array:
Array
(
    [apple] => 1
    [banana] => 1
    [peach] => 1
)

4. Complete example: Get data from URL and process it

Suppose we have an array of multiple URLs and want to extract the domain name of each URL and count the number of occurrences of each domain name. We can first use array_flip() to exchange the keys and values ​​of the array, and then use array_count_values() to count the frequency of each domain name.

 $urls = [
    'https://www.m66.net/product1',
    'https://www.m66.net/product2',
    'https://www.m66.net/product3',
    'https://www.example.net/product1',
    'https://www.m66.net/product4',
];

// Extract the domain name
$domains = array_map(function($url) {
    return parse_url($url, PHP_URL_HOST);
}, $urls);

// Reverse array
$flippedDomains = array_flip($domains);
print_r($flippedDomains);

// Statistics the number of occurrences of domain names
$countedDomains = array_count_values($flippedDomains);
print_r($countedDomains);

Output:

 Flipped Domains:
Array
(
    [m66.net] => https://www.m66.net/product1
    [example.net] => https://www.example.net/product1
)

Counted Domains:
Array
(
    [m66.net] => 4
    [example.net] => 1
)

In this example, the array_flip() function is used to exchange domain names and URLs. After that, we count the number of occurrences of each domain name through array_count_values() . The result shows that the m66.net domain name appears 4 times, while the example.net domain name appears only 1 time.

Hopefully this article helps you better understand how to use array_flip() and array_count_values() in combination to process array data. With the combination of these functions, you can easily reverse key-value pairs in the array and count the frequency of data, supporting further data analysis and processing.