📖 Scenario: You are organizing a small event and have two lists: one with guest names and another with their favorite drinks. You want to create a combined list that pairs each guest with their favorite drink.
🎯 Goal: Build a PHP script that merges two arrays into one associative array where each guest's name is linked to their favorite drink.
📋 What You'll Learn
Create two arrays with exact values for guests and drinks
Create a variable to hold the combined array
Use the array_combine function to merge the two arrays
Print the combined array using print_r
💡 Why This Matters
🌍 Real World
Combining related lists like names and preferences is common in event planning, surveys, and data organization.
💼 Career
Understanding how to merge arrays is useful for backend developers managing user data, preferences, or configuration settings.
Progress0 / 4 steps
1
Create the guests and drinks arrays
Create an array called $guests with these exact values: 'Alice', 'Bob', 'Charlie'. Also create an array called $drinks with these exact values: 'Coffee', 'Tea', 'Juice'.
PHP
Hint
Use square brackets [] to create arrays in PHP.
2
Create a variable for the combined array
Create a variable called $guest_drinks and set it to null for now.
PHP
Hint
Initialize the variable with null before assigning the combined array.
3
Combine the guests and drinks arrays
Use the array_combine function to combine $guests and $drinks into $guest_drinks.
PHP
Hint
The array_combine function takes two arrays: keys and values.
4
Print the combined array
Print the $guest_drinks array using print_r to show the combined list.
PHP
Hint
Use print_r($guest_drinks); to display the array in a readable format.
Practice
(1/5)
1. What does the PHP function array_merge do?
easy
A. Creates an associative array from two arrays
B. Joins two or more arrays into one combined array
C. Sorts an array in ascending order
D. Removes duplicate values from an array
Solution
Step 1: Understand the purpose of array_merge
The function array_merge takes multiple arrays and joins their elements into a single array.
Step 2: Compare with other functions
array_combine creates key-value pairs, sorting and removing duplicates are different functions.
Final Answer:
Joins two or more arrays into one combined array -> Option B
Quick Check:
array_merge joins arrays = B [OK]
Hint: array_merge joins arrays end-to-end [OK]
Common Mistakes:
Confusing array_merge with array_combine
Thinking array_merge sorts arrays
Assuming array_merge removes duplicates
2. Which of the following is the correct syntax to combine two arrays into an associative array using array_combine?
easy
A. array_merge($keys, $values);
B. array_combine($values, $keys);
C. array_combine($keys, $values);
D. array_combine($keys);
Solution
Step 1: Recall array_combine parameter order
The first parameter is the array of keys, the second is the array of values.
Step 2: Check each option
array_combine($keys, $values); correctly uses array_combine($keys, $values);. array_combine($values, $keys); reverses parameters, C uses wrong function, D misses second parameter.
Final Answer:
array_combine($keys, $values); -> Option C
Quick Check:
Keys first, values second in array_combine = A [OK]
Hint: Keys go first, values second in array_combine [OK]
A. array_combine merges arrays instead of combining keys and values
B. array_combine cannot use string keys
C. array_combine needs only one parameter
D. array_combine requires both arrays to have the same number of elements
Solution
Step 1: Check array_combine requirements
array_combine requires both arrays to have equal length to pair keys and values correctly.
Step 2: Analyze given arrays
$keys has 2 elements, $values has 1 element, causing an error.
Final Answer:
array_combine requires both arrays to have the same number of elements -> Option D
Quick Check:
Equal length arrays needed for array_combine = D [OK]
Hint: Keys and values arrays must be same length [OK]
Common Mistakes:
Ignoring array length mismatch
Thinking array_combine works with one array
Confusing array_combine with array_merge
5. Given two arrays: $keys = ['name', 'age', 'city']; $values = ['Alice', 30, 'Paris']; Which PHP code correctly creates an associative array and then merges it with ['country' => 'France']?