Name

array_keys()

Synopsis

    array array_keys ( array arr [, mixed search [, bool strict]] )

The array_keys() function takes an array as its only parameter, and returns an array of all the keys in that array. For example, if you have an array with user IDs as keys and usernames as values, you could use array_keys() to generate an array where the values were the user IDs. For example:

    $users[923] = 'TelRev';
    $users[100] = 'Skellington';
    $users[1202] = 'CapnBlack';
    $userids = array_keys($users);
    // $userids contains the values 923, 100, and 1202

There are two other parameters that can be passed to array_keys(): the value to match and a flag indicating whether to perform strict matching. These two allow you to filter your array keys—if you specify TelRev, then the only keys that array_keys() will return are the ones that have the value TelRev. By default, this is done by checking each key's value with the = = operator (is equal to); however, if you specify 1 as the third parameter, the check will be done with = = = (is identical to).

    $users[923] = 'TelRev';
    $users[100] = 'Skellington';
    $users[1202] = 'CapnBlack';
    $userids = array_keys($users, "TelRev");
    // userids contains only 923

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.