Home >Backend Development >PHP Tutorial >php-Arrays function-array_flip-exchange keys and values in array_PHP tutorial
array_flip() function exchanges the keys and values in the array
【Function】
This function will return a reversed array,
That is, the value of the original array becomes the key value of the new array, and the key value of the original array becomes the value of the new array
If there are the same values in the array, only the last one with the same value will be reversed to the new array
【Scope of use】
php4, php5.
【Use】
array array_flip( array trans )
trans/required/array to be reversed
【Example】
[php]
$array1 = array( "blue" => 6, "red" => 2, "green" => 3, "purple" => 4 );
$array2 = array( "blue" => 6, "red" => 4, "green" => 6, "purple" => 4 );
$array3 = array( "blue" => 6, "red" => 4, "green" => 6, "purple" => '' );
print_r( array_flip( $array1 ) );
print_r( array_flip( $array2 ) );
print_r( array_flip( $array3 ) );
/*
Array
(
[6] => blue
[2] => red
[3] => green
[4] => purple
)
Array
(
[6] => green
[4] => purple
)
Array
(
[6] => green
[4] => red
[] => purple
)
*/
Excerpted from zuodefeng’s notes