Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page