Grabbing keys and values

array array_keys ( array input [, mixed search_value [, bool strict]])

array array_values ( array input)

Array_keys() and array_values() are very closely related functions: the former returns an array of all the keys in an array, and the latter returns an array of all the values in an array. For example, consider you had 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 keys. Try this following example out:

<?php
    $users[923] = 'TelRev';
    $users[100] = 'Skellington';
    $users[1202] = 'CapnBlack';
    $userids = array_keys($users);
    var_dump($userids);
?>

After execution, $userids will be an array of user IDs, as you will see when the var_dump() function outputs $userids to the screen.

There are two other parameters that can be passed to array_keys(), which are, in order, a value to match, and whether to perform strict matching. These two are there to allow you to filter your array keys - if you specify "tennis", for example, then the only keys that array_keys() will return are the ones that have the value "tennis". 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).

<?php
    $users[923] = 'TelRev';
    $users[100] = 'Skellington';
    $users[1202] = 'CapnBlack';
    $userids = array_keys($users, "TelRev");
    var_dump($userids);
?>

This time the $userids array will only contain one element, with a value of 923, as the script filters by value "TelRev".

Moving on, the usefulness of the array_values() function may at first not be apparent - after all, why would you want the values of a new array to be set to the values of an old array? This all boils down to how numerical arrays are indexed. If you use the array operator [] to assign variables to an array, PHP will use 0, 1, 2, etc as the keys. If you then sort the array using a function such as asort(), which keeps the keys intact, the array's keys will be out of order because asort() sorts by value, not by key.

However, using the array_values() function makes PHP create a new array where the indexes are recreated and the values are copied from the old array, essentially making it renumber the array elements.

This next script demonstrates array_values() in action:

<?php
    $array[] = "Hello";
    $array[] = "World";
    $array[] = "Foo";
    $array[] = "Bar";
    $array[] = "Baz";
    var_dump($array);
    asort($array);

    var_dump($array);
    var_dump(array_values($array));
?>

The first var_dump() prints the array out in its original ordering. The second prints it out ordered by the values - the keys will be jumbled up, however. The final call to var_dump() uses array_values() to create a new array, and you should see that the jumbled up keys have been re-ordered. Here's how it looked on my screen:

array(5) { [0]=> string(5) "Hello" [1]=> string(5) "World" [2]=> string(3) "Foo" [3]=> string(3) "Bar" [4]=> string(3) "Baz" }
array(5) { [3]=> string(3) "Bar" [4]=> string(3) "Baz" [2]=> string(3) "Foo" [0]=> string(5) "Hello" [1]=> string(5) "World" }
array(5) { [0]=> string(3) "Bar" [1]=> string(3) "Baz" [2]=> string(3) "Foo" [3]=> string(5) "Hello" [4]=> string(5) "World" }

You should use array_values() when you want to re-order an array's indexes either because they are jumbled up or because they have holes in, but you can also use it to convert an associative array with strings as the indexes to a plain numerical array.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Randomising your array >>

Previous chapter: Sorting arrays

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.