Current Location: Home> Latest Articles> Use var_dump() and print_r() to check the effect of array_flip()

Use var_dump() and print_r() to check the effect of array_flip()

M66 2025-06-03

In PHP, array_flip() is a very practical function for exchanging keys and values ​​of an array. Assuming you have an array where keys and values ​​are interchangeable, this function can easily achieve this. However, in order to better understand and debug the effects of the array_flip() function, it is very important to use var_dump() and print_r() to output the structure and content of the array.

This article will show how to combine var_dump() and print_r() to check the effect of the array_flip() function.

1. Use array_flip() function

The array_flip() function takes an array as input and returns a new array, where the key of the original array becomes a value and the value of the original array becomes a key.

For example, suppose you have the following array:

 <?php
$array = array("a" => "apple", "b" => "banana", "c" => "cherry");
?>

After calling array_flip() , the keys and values ​​of the array will be exchanged:

 <?php
$flippedArray = array_flip($array);
?>

2. Use var_dump() to output arrays

The var_dump() function is a very useful debugging tool in PHP. It displays the detailed structure of an array or object, including types and values.

Let's use var_dump() to see the effect of array_flip() :

 <?php
var_dump($flippedArray);
?>

The output will look like this:

 array(3) {
  ["apple"] => string(1) "a"
  ["banana"] => string(1) "b"
  ["cherry"] => string(1) "c"
}

From this output, you can see that the value of the original array has become the key of the new array, and the key of the original array has become the value of the new array.

3. Use print_r() to output array

print_r() is another debugging function in PHP. It is similar to var_dump() , but the output format is more concise and suitable for viewing array content.

Similarly, we can use print_r() to check the effect of array_flip() :

 <?php
print_r($flippedArray);
?>

Output result:

 Array
(
    [apple] => a
    [banana] => b
    [cherry] => c
)

You can see that the format of print_r() output is relatively concise, suitable for quickly viewing keys and values ​​of arrays.

4. Use var_dump() and print_r() to debug different situations

In actual development, you may encounter some different situations, such as whether the value of the original array is unique, or whether there are duplicate values. array_flip() can only handle cases where values ​​are unique. If the values ​​of the array are repeated, array_flip() will lose some of these keys.

Suppose we have an array containing duplicate values:

 <?php
$arrayWithDuplicates = array("a" => "apple", "b" => "apple", "c" => "cherry");
$flippedArrayWithDuplicates = array_flip($arrayWithDuplicates);
?>

Use var_dump() or print_r() to output the result:

 <?php
var_dump($flippedArrayWithDuplicates);
?>

Output result:

 array(2) {
  ["apple"] => string(1) "b"
  ["cherry"] => string(1) "c"
}

As you can see, since apple appears twice, array_flip() only retains the value corresponding to the last key b .

5. Summary

By using var_dump() and print_r() , we can see more clearly how the array_flip() function handles arrays, helping us quickly understand the structure and content of arrays during debugging. These two debugging functions have their own characteristics, var_dump() is more detailed, while print_r() is more concise. Select the appropriate function according to the requirements for debugging.