Skip to Content
Course content

92: array_keys and array_values

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Up until now, we've treated associative arrays as these handy pairs where the key tells us what the value is. But in the real world, you'll often run into situations where you only care about one half of that relationship. Maybe you need a list of all the IDs to pass into a database query, or maybe you need a clean list of names to populate a dropdown menu without the IDs getting in the way.

Setting up our coffee menu

Let's imagine we're building a small point-of-sale system for a cafe. I'll start with a simple associative array where the keys are the internal product codes and the values are the human-readable names. It's a clean way to organize the data.

$coffee_menu = [
    'esp' => 'Espresso',
    'lat' => 'Latte',
    'cap' => 'Cappuccino',
    'ame' => 'Americano',
    'mac' => 'Macchiato'
];

Isolating the product codes

Now, imagine I need to send a request to a warehouse API to check the stock levels for all these items. The API doesn't want the names; it just wants a simple list of the codes. This is where array_keys() comes in. It strips away the values and gives us a brand new, numerically indexed array containing only the keys.

$product_codes = array_keys($coffee_menu);

// $product_codes is now: ['esp', 'lat', 'cap', 'ame', 'mac']
echo implode(', ', $product_codes); 
// Output: esp, lat, cap, ame, mac

The "index zero" mistake

Here is something I've done more times than I'd like to admit. I wanted to grab the first item in my menu to use as a "Featured Drink" on the homepage. Since I'm used to standard arrays, I tried this:

// This is where I messed up
echo "Today's special is: " . $coffee_menu[0];

Of course, this throws an "Undefined array key 0" error. Why? Because $coffee_menu doesn't have an index 0; its first index is 'esp'. I can't rely on numerical offsets with associative arrays. To fix this, I can use array_values() to create a temporary version of the array that is indexed from 0, 1, 2, and so on.

$names_only = array_values($coffee_menu);
echo "Today's special is: " . $names_only[0]; 
// Output: Today's special is: Espresso

Cleaning up for the frontend

Finally, let's say we need to send a list of the drink names to a JavaScript frontend. The frontend doesn't care about our internal codes; it just needs a clean array of strings to map over and create <li> tags. Instead of writing a foreach loop to push values into a new array, array_values() does the heavy lifting in one line.

$display_list = array_values($coffee_menu);

// Now we have a clean, sequential array:
// ['Espresso', 'Latte', 'Cappuccino', 'Americano', 'Macchiato']

It's a simple tool, but it saves you from writing a lot of boilerplate loops. Just remember: array_keys() for the identifiers, array_values() for the data.




📋 Practical Task

Building a User Role Permission Mapper

You are building a permissions system. You have an associative array where the keys are the user roles (e.g., 'admin', 'editor') and the values are the permission levels (e.g., 'all', 'limited').

Your Task:

  • Create an associative array named $role_permissions with at least four different roles and their corresponding permission levels.
  • Use array_keys() to create a variable called $all_roles and print it using print_r() to verify you have a simple list of roles.
  • Use array_values() to create a variable called $all_levels and print it using print_r() to verify you have a simple list of permission levels.
  • Using the $all_roles array you created, print only the second role in the list to the screen.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.