0
0
PHPprogramming~15 mins

Associative array creation in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Associative Array Creation in PHP
📖 Scenario: You are creating a small contact list for your friends. Each friend has a name and a phone number.
🎯 Goal: Build an associative array in PHP that stores friends' names as keys and their phone numbers as values.
📋 What You'll Learn
Create an associative array called contacts with exact key-value pairs
Add a variable called new_contact set to 'Bob'
Use the array_key_exists function to check if a contact exists
Print the phone number of the new_contact if it exists
💡 Why This Matters
🌍 Real World
Associative arrays are used to store data with named keys, like contact lists, settings, or user profiles.
💼 Career
Knowing how to create and use associative arrays is essential for backend web development and data handling in PHP.
Progress0 / 4 steps
1
Create the contacts associative array
Create an associative array called contacts with these exact entries: 'Alice' => '123-4567', 'Bob' => '234-5678', and 'Charlie' => '345-6789'.
PHP
Need a hint?

Use square brackets [] to create the array and separate key-value pairs with =>.

2
Add a new_contact variable
Add a variable called new_contact and set it to the string 'Bob'.
PHP
Need a hint?

Use = to assign the string 'Bob' to the variable new_contact.

3
Check if new_contact exists in contacts
Use an if statement with array_key_exists to check if new_contact is a key in contacts. Inside the if, assign the phone number to a variable called phone_number.
PHP
Need a hint?

Use array_key_exists($new_contact, $contacts) inside the if condition.

4
Print the phone number
Print the value of phone_number using echo.
PHP
Need a hint?

Use echo $phone_number; to display the phone number.