Name

array_flip()

Synopsis

    array array_flip ( array arr )

The array_flip() function takes an array as its parameter, and exchanges all the keys in that array with their matching values, returning the new, flipped array. You can see how it works in this script:

    $capitalcities['England'] = 'London';
    $capitalcities['Scotland'] = 'Edinburgh';
    $capitalcities['Wales'] = 'Cardiff';
    $flippedcities = array_flip($capitalcities);
    var_dump($flippedcities);

The output is this:

    array(3) {
            ["London"]=>
            string(7) "England"
            ["Edinburgh"]=>
            string(8) "Scotland"
            ["Cardiff"]=>
            string(5) "Wales"
    }

As you can see, London, Edinburgh, and Cardiff are the keys in the array now, with England, Scotland, and Wales as the values.

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.