0
0
PHPprogramming~15 mins

Array merge and combine in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Array merge and combine
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print_r($guest_drinks); to display the array in a readable format.