PHP | array_flip() Function
This built-in function of PHP is used to exchange elements within an array, i.e., exchange all keys with their associated values in an array and vice-versa. We must remember that the values of the array need to be valid keys, i.e. they need to be either integer or string. A warning will be thrown if a value has the wrong type, and the key/value pair in question will not be included in the result.
Examples:
Input : array = ("aakash" => 20, "rishav" => 40, "gaurav" => 60)
Output :
Array
(
[20] => aakash
[40] => rishav
[60] => gaurav
)
Explanation: The keys and values are exchanged and the last key or value is taken.
Input : array = ("aakash" => "rani", "rishav" => "sristi",
"gaurav" => "riya", "laxman" => "rani")
Output :
Array
(
[rani] => laxman
[sristi] => rishav
[riya] => gaurav
)
Syntax:
array array_flip($array)
Parameters: The function takes only one parameter $array that refers to the input array.
Return Type: This function returns another array, with the elements exchanged or flipped, it returns null if the input array is invalid.
Below program illustrates the working of array_flip():
<?php // PHP function to illustrate the use of array_flip() function Flip($array) { $result = array_flip($array); return($result); } $array = array("aakash" => "rani", "rishav" => "sristi", "gaurav" => "riya", "laxman" => "rani"); print_r(Flip($array)); ?> |
Output:
Array
(
[rani] => laxman
[sristi] => rishav
[riya] => gaurav
)
References:
http://php.net/manual/en/function.array-flip.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- PHP | end() Function
- CSS | url() Function
- PHP | Ds\Set first() Function
- p5.js | box() Function
- PHP | key() Function
- D3.js | d3.map.set() Function
- PHP Ds\Set sum() Function
- PHP | pos() Function
- PHP | Ds\Set contains() Function
- PHP Ds\Set get() Function
- PHP | Ds\Map first() Function
- PHP Ds\Map sum() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

