array_flip in PHP

Syntax of array_flip in PHP

array array_flip(array array)

Returns an array in which the elements’ keys are the original array’s values, and vice versa. If multiple values are found, the last one encountered is retained. If any of the values in the original array are any type except strings and integers, array_flip() will issue a warning, and the key/value pair in question will not be included in the result. array_flip() returns NULL on failure

Example of array_flip in PHP

<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);

print_r($flipped);
?>

Output

Array
(
    [oranges] => 0
    [apples] => 1
    [pears] => 2
)

Leave a Comment