array_flip

Exchanges all keys with their associated values in an array

Syntax

array_flip ( array $array ) : array

Parameters

array

An array of key/value pairs to be flipped.

Return

Returns the flipped array on success and NULL on failure.

Examples

Indexed

<?

$array = array("a", "b", "c");

$return = array_flip($array);

print_r($return);

?>
Array
(
    [a] => 0
    [b] => 1
    [c] => 2
)

Associative

<?

$array = array("a" => 0, "b" => 1, "c" => 2);

$return = array_flip($array);

print_r($return);

?>
Array
(
    [0] => a
    [1] => b
    [2] => c
)

Collision

<?

$array = array("a" => 0, "b" => 0, "c" => 2);

$return = array_flip($array);

print_r($return);

?>
Array
(
    [0] => b
    [2] => c
)

PHP

Home Menu