Swapping keys and values of an array in PHP using array_flip function

The array_flip() function in PHP is used to exchange the keys and values of an array. When this function is applied to an array, each key becomes its corresponding value, and each value becomes its corresponding key in the resulting array. It is important to note that this only works correctly if the values in the original array are either strings or integers and are unique. If the array contains duplicate values, only the last matching key will be preserved in the flipped array, and the previous ones will be overwritten. Example is mentioned below:


<?php
$arr_obj2 = [
    "1" => "Red",
    "2" => "Green"
];

$arr_flipped = array_flip($arr_obj2);

// below is out of flipped array
$arr_flipped  = [
    "Red" => "1",
    "Green" => "2"
];