PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
92: array_keys and array_values
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_permissionswith at least four different roles and their corresponding permission levels. - Use
array_keys()to create a variable called$all_rolesand print it usingprint_r()to verify you have a simple list of roles. - Use
array_values()to create a variable called$all_levelsand print it usingprint_r()to verify you have a simple list of permission levels. - Using the
$all_rolesarray you created, print only the second role in the list to the screen.
There are no comments for now.