PHP Tutorial - PHP array_flip() function






Definition

The array_flip() function takes an array as its parameter, and exchanges all the keys in that array with their values, returning the new, flipped array.

Syntax

PHP array_flip() function has the following syntax.

array array_flip ( array arr )

Parameter

ParameterIs RequiredDescription
arrRequired.Array to flip

Return

PHP array_flip() function returns the new, flipped array

Example


<?PHP/* www .j  av  a  2s.  c  o  m*/
$capitalcities['A'] = 'apple';
$capitalcities['B'] = 'better';
$capitalcities['W'] = 'java2s.com';
$flippedcities = array_flip($capitalcities);
var_dump($flippedcities);


$a2=array("d"=>"D","b"=>"B","e"=>"E","j"=>"java2s.com");
$a2 = array_flip($a2);
var_dump($a2);
?>

The code above generates the following result.