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/* w w  w  .j  av a 2s.  co  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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions