array_flip() function in PHP


The array_flip() function exchanges all keys with their associated values in an array. It returns flipped array on success, else NULL on failure.

Syntax

array_flip(arr)

Parameters

  • arr − Specify the array of key/value pairs to be flipped.

Return

The array_flip() function returns flipped array on success, else NULL on failure.

Example

Live Demo

<?php
   $arr = array("p"=>"keyboard","q"=>"mouse","r"=>"monitor");
   $res = array_flip($arr);
   print_r($res);
?>

Output

Array
(
   [keyboard] => p
   [mouse] => q
   [monitor] => r
)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements