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

PHP strripos() Function The strripos() function in PHP finds the position of the last occurrence of a substring in the specified string. It is a case-insensitive and binary-safe function. Syntax strripos ( string $haystack , mixed $needle [, int $offset ] ) Parameter haystack(required)- This parameter signifies the...

1 minute 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 Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP settype() Function

PHP settype() Function The settype() function in PHP is used to set the type of variable var to type. Syntax settype ( mixed &$var , string $type ) Parameter var(required)- This parameter represents the variable being converted. type(required)- This parameter represents the type of variable that is needed.  The possible values of type are...

1 minute read.

PHP vfprintf() Function

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

3 minutes read.

PHP array_intersect_uassoc() function

The array_intersect_uassoc () function in PHP is used to compute the intersection of arrays with the help of an additional index check, comparing indexes by a callable function. It returns an array containing...

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

PHP shuffle() Function The shuffle() function in PHP shuffles an array in a random array. Syntax shuffle ( array &$array )  Parameter array(required)- This parameter represents the input array.  Return This parameter returns a Boolean value TRUE on success or FALSE on failure. Example 1 Output Original Array: Array ...

1 minute read.

PHP array_slice() Function

PHP array_slice() Function The array_slice() function in PHP is used to extract a slice of the array. It returns the sequence of elements from the array as specified by the offset and length parameters. Syntax array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) Parameter array(required)- This array represents the...

2 minutes read.

PHP array_intersect_key() function

The array_intersect_key () function in PHP is used to compute the intersection of arrays using keys for comparison. It returns an array containing all the entries from array1 which have keys that are present in...

1 minute read.

PHP strchr() Function

PHP strchr() Function The strchr() function in PHP searches for the first occurrence of the specified string inside another string. It is a binary-safe case -sensitive function and is the alias of strstr() function. Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] ) Parameter haystack(required)-...

1 minute read.

PHP str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

1 minute read.

PHP str_shuffle() Function

PHP str_shuffle() Function The str_shuffle() function in PHP randomly shuffles all the characters of the specified string. Syntax str_shuffle ( string $str ) Parameter str(required)- This parameter represents the input string to shuffle. Return This function returns the shuffled string. Example 1 ...

1 minute read.

PHP addcslashes() Function

The addcslashed() function in PHP is used to add quote string with slashes in a C style. It is case-sensitive function.  Syntax addcslashes (str, charlist ) Parameter str(required)- This parameter represents the string to be escaped. charlist(required)-...

1 minute read.

PHP ord() Function

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.

PHP stripslashes() Function

PHP stripslashes() Function The stripslashes() Function in PHP removes backslashes or un-quotes a quoted string. Syntax stripslashes ( string $str ) Parameter str(required)- This parameter signifies the input string. Return This function returns a string with backslashes stripped off and the double backslashes (\\) are made...

1 minute read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies 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 array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP isset() function

PHP isset is used to check whether the variable is set or not.The PHP isset() function returns false if variable contains a NULL value. Syntax: isset(variable1, variable2….) Presentation of PHP isset: In...

2 minutes read.