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 Data Types

PHP data type is used to hold the various types of data or value.  There are three types of data types in PHP. It is a loosely data typed language, so...

2 minutes read.

PHP array_sum() Function

PHP array_sum() Function The array_sum() function in PHP is used to calculate the sum of values in an array. Syntax array_sum ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns the sum of values as an...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

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 is_integer() Function

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

1 minute read.

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP substr_replace() Function

PHP substr_replace() Function The substr_replace() function in PHP replaces a part of a string with another string. Syntax substr_replace (mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) Parameter string(required)- This parameter represents the input string. replacement(required)- This parameter signifies the string to replace with. start(required)-...

2 minutes read.

PHP quotemeta() Function

PHP quotemeta() Function The quotemeta() function in PHP quotes the meta characters and returns a string with a backslash character (\) before some predefine characters. The predefined characters are as follows: period ...

1 minute read.

PHP money_format() Function

PHP money_format() Function The money_format() function in PHP formats a number as a currency string and returns a formatted version of number. Syntax money_format ( string $format , float $number )  Parameter format(required)- This parameter specifies the string to be formatted and how to format the...

1 minute read.

PHP convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

1 minute read.

PHP array_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP natcasesort() Function

PHP natcasesort() Function The natcasesort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natcasesort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.

PHP nl_langinfo() Function

PHP nl_langinfo() Function The nl_langinfo() function in PHP returns specific local information. This function does not work on Windows platforms. Syntax nl_langinfo ( int $item ) Parameter Item(required)- This parameter specifies the  integer value of the element or the constant name of the...

1 minute read.

PHP echo() Function

PHP echo() Function The echo() function in PHP outputs or displays one or more strings. The major differences between print() function and echo() function is that echo accepts an argument list and doesn't have a return value. Also, it is slightly...

1 minute read.

PHP levenshtein() Function

PHP levenshtein() Function The levenshtein() function in PHP calculates the Levenshtein distance between two strings. Syntax levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )  Parameter str1(required)- This parameter signifies one of the strings being evaluated for Levenshtein distance. str2(required)- This parameter signifies one of the strings being evaluated...

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 is_string() Function

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

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.