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
90: array_merge and array_combine
I'm currently working on a feature for a blog platform where I need to handle post tags. The goal is to take a set of "mandatory" tags (defined by the admin) and combine them with the tags the author chose. It sounds simple, but PHP's way of handling array combinations can be a bit slippery if you aren't paying attention to the keys.
The additive operator trap
My first instinct was to just use the plus operator. I've seen people do it, and it feels intuitive. Let's see what happens:
$mandatoryTags = ['Programming', 'WebDev'];
$userTags = ['PHP', 'Backend'];
$allTags = $mandatoryTags + $userTags;
print_r($allTags);
At first glance, this looks fine. I get both sets of tags. But then I tried it with a different set of data where the user tags were somehow indexed differently, or I tried to merge them in reverse. I quickly realized that the + operator doesn't actually "merge" in the way most people expect—it's a union. If a key exists in the first array, the second array's value for that key is simply ignored. Since these are indexed arrays, they both start at key 0. The + operator sees key 0 in $mandatoryTags and decides it doesn't need key 0 from $userTags.
That's a recipe for lost data. This is where array_merge() comes in.
Letting array_merge handle the heavy lifting
I swapped out the plus sign for the function call, and the behavior changed immediately:
$mandatoryTags = ['Programming', 'WebDev'];
$userTags = ['PHP', 'Backend'];
$allTags = array_merge($mandatoryTags, $userTags);
print_r($allTags);
// Output: [0 => 'Programming', 1 => 'WebDev', 2 => 'PHP', 3 => 'Backend']
Now it's actually appending the elements and re-indexing them. This is exactly what I wanted for a list of tags. But wait—what happens if I'm merging associative arrays, like user settings? I tried this:
$defaultSettings = ['theme' => 'light', 'notifications' => true];
$userSettings = ['theme' => 'dark'];
$finalSettings = array_merge($defaultSettings, $userSettings);
print_r($finalSettings);
// Output: ['theme' => 'dark', 'notifications' => true]
Notice how 'theme' was overwritten? With string keys, array_merge doesn't append; it overwrites. This is actually a superpower for things like configuration files where you want user preferences to override system defaults.
Pairing separate lists with array_combine
Now, I've hit a different problem. My database is returning two separate arrays for a list of categories: one array of IDs and one array of Category Names. They are perfectly aligned by index, but they are useless to me as two separate lists. I need them as a single associative array where the ID is the key.
I could write a foreach loop, but that feels like overkill for this. I remember there's a specific tool for this "zipping" action: array_combine().
$categoryIds = [10, 22, 45];
$categoryNames = ['News', 'Tutorials', 'Reviews'];
$categoryMap = array_combine($categoryIds, $categoryNames);
print_r($categoryMap);
/*
Output:
Array (
[10] => News
[22] => Tutorials
[45] => Reviews
)
*/
It's clean and readable. One thing I noticed while testing this: if the two arrays aren't the exact same length, array_combine will throw a ValueError. It's strict. If you're pulling this data from an external API, you'd probably want to wrap this in a check to ensure count($categoryIds) === count($categoryNames) before attempting the combine.
📋 Practical Task
Build a User Profile Data Mapper
You are receiving raw data from two different legacy API endpoints. One endpoint provides a list of attribute keys, and the other provides the corresponding values for a specific user. Additionally, you have a set of default profile attributes that must be applied if the user hasn't specified them.
Your task:
- Create an array
$keys = ['first_name', 'last_name', 'timezone', 'language']. - Create an array
$values = ['Jane', 'Doe', 'UTC', 'English']. - Use
array_combineto merge these into a$userProfileassociative array. - Create a
$defaultsarray containing'theme' => 'light'and'language' => 'Spanish'. - Use
array_mergeto combine the$defaultsand the$userProfileso that the user's actual data overrides the defaults. - Print the final resulting array to the screen.
There are no comments for now.