0
0
PHPprogramming~15 mins

Array key and value extraction in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Array key and value extraction
📖 Scenario: You have a list of products with their prices stored in an array. You want to separate the product names and their prices into two different arrays for easier use later.
🎯 Goal: Build a PHP script that extracts the keys (product names) and values (prices) from an associative array into two separate arrays.
📋 What You'll Learn
Create an associative array called $products with exact entries: 'Apple' => 150, 'Banana' => 50, 'Cherry' => 200
Create a variable called $productNames to hold the keys from $products
Create a variable called $productPrices to hold the values from $products
Print the $productNames array
Print the $productPrices array
💡 Why This Matters
🌍 Real World
Extracting keys and values from arrays is common when working with data like product lists, user information, or settings.
💼 Career
Many jobs require handling associative arrays or dictionaries to organize and process data efficiently.
Progress0 / 4 steps
1
Create the products array
Create an associative array called $products with these exact entries: 'Apple' => 150, 'Banana' => 50, 'Cherry' => 200.
PHP
Need a hint?

Use square brackets [] to create the array and separate each key-value pair with a comma.

2
Extract the product names
Create a variable called $productNames and set it to the keys of the $products array using the array_keys() function.
PHP
Need a hint?

Use the array_keys() function to get all keys from an array.

3
Extract the product prices
Create a variable called $productPrices and set it to the values of the $products array using the array_values() function.
PHP
Need a hint?

Use the array_values() function to get all values from an array.

4
Print the product names and prices
Print the $productNames array and then print the $productPrices array using print_r().
PHP
Need a hint?

Use print_r() to display arrays in a readable format.