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

PHP strstr() Function The strstr() function in PHP finds the first occurrence of a string. It returns part of the parameter haystack string starting from and including the first occurrence of needle to the end of the haystack parameter. This function is case-sensitive.  Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] )  Parameter  haystack(required)-...

2 minutes read.

PHP bin2hex() Function | Convert Binary to Hexadecimal

PHP bin2hex() Function The bin2hex() function in PHP converts the specified string value of ASCII characters to hexadecimal value. Syntax bin2hex(str) Parameter str(Required): This parameter represents the string to be converted into hexadecimal. Return  This function returns...

1 minute 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 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 array_multisort() Function

PHP array_multisort () Function The array_multisort() function in PHP sorts multiple or multi-dimensional arrays at once. Syntax array_multisort ( array &$array1 [, mixed $array1_sort_order [, mixed $array1_sort_flags [, mixed $... ]]] ) Parameter array1(required)- This parameter specified the input array to sort. array1_sort_order(optional)- This parameter specifies the order (ascending or descending)...

1 minute read.

PHP join() Function

PHP join() Function The join() function of PHP returns a string from the elements of an array. This function is an alias of the implode() function. Syntax join(separator,array) Parameter separator(Optional)- Specifies what to put between the array elements. The default...

1 minute read.

PHP array_diff_assoc() Function

PHP array_diff_assoc() Function The array_diff_assoc() Function in PHP compares the difference of arrays and returns an array containing all the values from array1 that are not present in any of the other arrays. Syntax array_diff_assoc ( array $array1 , array $array2 [, array $... ] ) Parameter array1(required)- This parameter signifies...

1 minute read.

PHP str_split() Function

PHP str_split() Function The str_split() function in PHP splits a string into an array. String str_split ( string $string [, int $split_length = 1 ] ) Parameter string(required)- This parameter represents the input string. split_length(optional)- This parameter signifies the maximum length of the chunk. The default value is 1. Return This function...

2 minutes read.

PHP Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

PHP key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP chr() Function

PHP chr() Function The chr() function in PHP generates a single byte string from a number and returns a one-character string containing the character as specified by the unsigned integer. Syntax chr(bytevalue);    Parameter bytevalue(required)- This...

1 minute read.

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...

1 minute read.

PHP sprintf() Function

PHP sprintf() Function The sprintf() function in PHP is used to write a formatted string to a variable and returns a formatted string. PHP 4 and above versions support the sprintf() function. The related functions are...

8 minutes read.

PHP array_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute read.

PHP array_intersect_assoc() function

PHP array_intersect_assoc() function The array_intersect_assoc() function in PHP is used to compute the intersection of arrays with the help of an additional index check. It returns an array containing all the entries from array1 that are present...

1 minute read.

PHP natsort() Function

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

1 minute read.

PHP array_key_exists() function

The array_key_exists () function in PHP is used to check whether the specified key exists in the array or not. It returns a Boolean value TRUE if the given key exists else it returns FALSE. Syntax array_key_exists(mixed key,array$array) Parameter key(required)- This...

1 minute read.

PHP range() Function

PHP range() Function The range () function in PHP is used to create an array containing a range of elements. Syntax range ( mixed $start , mixed $end [, number $step = 1 ] ) Parameter start(required)- This parameter represents the first value of the sequence. end(required)- This parameter represents the...

1 minute read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

PHP array_column() Function

PHP array_column() Function The array_column() function in PHP is used to return the values from a single column specified in the input array. Syntax array_column (array $input, mixed $column_key [, mixed $index_key]) Parameter input(required)- This parameter represents a multi-dimensional array or an array of...

1 minute read.