PHP array_flip() function

PHP array_flip() function

The array_flip() function in PHP exchanges all keys with their associated values in an array and returns an array in flip order.

If the specified value has several occurrences, the latest key will be used as its value, and all others will be lost.

Syntax

array_flip ( array $array )

Parameter

array(required)- This parameter represents an input array of key/value pairs to be flipped.

Return

This function returns an array in flip order on success or returns NULL on failure.

Example 1

 

Output

Original array: 
 Array
 (
     [0] => India
     [1] => China
     [2] => Japan
     [3] => Sri Lanka
 ) 
  Flipped array: 
 Array
 (
     [India] => 0
     [China] => 1 
     [Japan] => 2
     [Sri Lanka] => 3
 ) 

Example 2

"apple", "g"=>"grapes", "b"=>"berry", "s"=>"Strawberry");
 echo("Original array: \n");
 print_r($input_array);
 //flipping the array keys and values
 $flipped_array = array_flip($input_array); 
 echo("\n Flipped array: \n");
 print_r($flipped_array);
 ?> 

Output

Original array: 
 Array
 (
     [a] => apple
     [g] => grapes
     [b] => berry
     [s] => Strawberry 
 )
  Flipped array: 
 Array
 (
     [apple] => a
     [grapes] => g
     [berry] => b 
     [Strawberry] => s
 ) 

Example 3

"A", "g"=>"G", "b"=>"B","s"=>"S");
 echo("Original array: \n");
 print_r($input_array);
 //flipping the array keys and values
 $flipped_array = array_flip($input_array); 
 echo("\n Flipped array: \n");
 print_r($flipped_array);
 ?> 

Output

Original array: 
 Array
 (
     [a] => A
     [g] => G
     [b] => B
     [s] => S 
 )
  Flipped array: 
 Array
 (
     [A] => a
     [G] => g
     [B] => b 
     [S] => s
 ) 

Example 4

"A", "g","G", "b","B");
 echo("Original array: \n");
 print_r($input_array);
 //flipping the array keys and values
 $flipped_array = array_flip($input_array); 
 echo("\n Flipped array: \n");
 print_r($flipped_array);
 ?> 

Output

Original array: 
 Array
 (
     [a] => A
     [0] => g
     [1] => G
     [2] => b
     [3] => B 
 )
  Flipped array: 
 Array
 (
     [A] => a
     [g] => 0
     [G] => 1 
     [b] => 2
     [B] => 3
 ) 

Related Topics

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP printf() Function

PHP printf() Function The printf() function in PHP outputs a formatted string. The related functions of printf() function are as follows: sprintf()vprintf()vsprintf()fprintf()vfprintf() Syntax printf ( string $format [, mixed $... ] )  Parameter format(required)- This parameter formats the string which is composed of zero or...

1 minute read.

Composer Installation

Composer is an application-level package manager for the PHP Programming language. The development began in April 2011 and it was first released on March 1, 2012. Commands: require install update remove Installation:  Download the installer by visiting...

1 minute read.

PHP krsort() Function

PHP krsort() Function The krsort() function in PHP sorts an array by key in reverse order while maintaining the keys. Syntax krsort (array & $array[,int $sort_flags= SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This...

3 minutes read.

PHP array_unique() Function

PHP array_unique() Function The array_unique() function in PHP removes the duplicate values from the specified array. Syntax array_unique (array $array [, int $sort_flags = SORT_STRING]) Parameter array(required)- This array represents the first input array. sort_flags (optional)- This parameter is used to modify the sorting behavior. The...

1 minute read.

PHP count() function

PHP count() function The count() function in PHP is used to count all elements in the specified array or something in an object. Syntax count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )   Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)- This...

2 minutes read.

PHP is_array() Function

The is_array() Function in PHP is used to  find whether a variable is an array or not. It returns a Boolean value true if the parameter var is an array else it returns...

1 minute read.

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP list() Function

PHP list() Function The list() function in PHP sorts an array by key while maintaining the keys. Syntax list ( mixed $var1 [, mixed $... ] ) Parameter var1(required)- This parameter accepts variables separated by spaces. …(optional)- This parameter accepts a list of variables separated...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP strtolower() Function

The strtolower() function in PHP is used to convert a string to lowercase. This function is binary-safe. The related functions are as follows: strtoupper()lcfirst()ucfirst()ucwords() Syntax strtolower ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

PHP is_float() Function

The PHP is_float() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP print_r() Function

PHP print_r() Function The print_r() function in PHP displays the information stored in a variable. Syntax print_r (mixed $expression [, bool $return] ) Parameter expression(required)- This parameter represents the expression to be printed. return(optional)- This parameter is used to store the output of...

2 minutes read.

PHP soundex() Function

PHP soundex() Function The soundex() function in PHP Calculates the soundex key(four character long alphanumeric string that represent English pronunciation of a word) of a string. Syntax soundex ( string $str ) Parameter str(required) – This parameter represents the input string. Return This function returns the soundex...

1 minute read.

PHP is_resource() Function

PHP is_resource() Function The is_resource() function in PHP finds whether a variable is a resource or not. It returns a Boolean value TRUE if the parameter var is resource, else it returns FALSE Syntax is_resource ( mixed $var ) Parameter var(required)- This parameter represents the value to check. Return This function...

1 minute read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP sort() Function

PHP sort() Function The sort() function in PHP sorts an array where the elements will be arranged from lowest to highest. Syntax sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the...

1 minute read.

PHP convert_cyr_string() Function

PHP convert_cyr_string() Function The convert_cyr_string() function in PHP converts from one Cyrillic character-set to another. The supported characters set are given below: k - koi8-rw - windows-1251i - iso8859-5a - x-cp866d - x-cp866m...

1 minute read.