Current Location: Home> Latest Articles> How to use array_flip() and array_filter() functions to invert and filter specific elements in an array?

How to use array_flip() and array_filter() functions to invert and filter specific elements in an array?

M66 2025-06-03

In PHP, array_flip() and array_filter() are two very useful array operation functions. array_flip() is used to swap keys and values ​​in an array, while array_filter() is used to filter elements in an array based on conditions. Combining these two functions, we can perform more complex operations on the array, such as inverting the array and filtering out elements that meet specific conditions at the same time.

This article will explain how to use these two functions to invert and filter specific elements in an array. Let's understand this process with a concrete example.

1. Use array_flip() function to invert array

The array_flip() function exchanges keys and values ​​in an array. After swap, the original array value becomes a key, and the original key becomes a value. For example, the following code shows how to use the array_flip() function to invert a simple array:

 <?php
// Original array
$array = array(
    "apple" => "fruit",
    "carrot" => "vegetable",
    "banana" => "fruit",
);

// Invert the array
$flipped_array = array_flip($array);

// Print the inverted array
print_r($flipped_array);
?>

The output result is:

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

As you can see, the array_flip() function turns the original key ( apple , carrot , banana ) into a new value, and the original value ( fruit , vegetable ) into a new key.

2. Use array_filter() function to filter array elements

The array_filter() function filters elements in an array based on specified conditions. It receives two parameters: the array to be filtered and a callback function. The callback function returns true or false to determine whether to retain the corresponding element.

Here is an example showing how to use array_filter() to filter out elements with a value of "fruit" :

 <?php
// Original array
$array = array(
    "apple" => "fruit",
    "carrot" => "vegetable",
    "banana" => "fruit",
);

// use array_filter The filter value is "fruit" Elements
$filtered_array = array_filter($array, function($value) {
    return $value == "fruit";
});

// Print filtered array
print_r($filtered_array);
?>

The output result is:

 Array
(
    [apple] => fruit
    [banana] => fruit
)

As you can see, the array_filter() function retains all elements with the value "fruit" and deletes other elements.

3. Use array_flip() and array_filter() functions in combination

Now we can use the array_flip() and array_filter() functions in combination. Suppose we have an array containing multiple key-value pairs, and we want to invert the array and filter out elements with the value "fruit" .

 <?php
// Original array
$array = array(
    "apple" => "fruit",
    "carrot" => "vegetable",
    "banana" => "fruit",
    "tomato" => "vegetable",
);

// 先Invert the array
$flipped_array = array_flip($array);

// 然后use array_filter Filter out key as "fruit" Elements
$filtered_array = array_filter($flipped_array, function($key) {
    return $key == "fruit";
});

// Print results
print_r($filtered_array);
?>

The output result is:

 Array
(
    [apple] => fruit
    [banana] => fruit
)

In this example, we first use the array_flip() function to invert the array, and then use array_filter() to filter out the elements whose key is "fruit" after inversion. In this way, we can get an array that meets the criteria.

4. Summary

By combining array_flip() and array_filter() , we can implement complex array operations. First, the key value pairs of the array are inverted through array_flip() , and then the elements that meet specific conditions are filtered out using array_filter() . Such methods are very suitable for use when dealing with arrays with complex structures.