📖 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
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
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
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
Hint
Use print_r() to display arrays in a readable format.
Practice
(1/5)
1. Which PHP function is used to extract all the keys from an array?
easy
A. array_map()
B. array_values()
C. array_keys()
D. array_filter()
Solution
Step 1: Understand the function purpose
array_keys() returns all keys from an array.
Step 2: Compare with other functions
array_values() returns values, not keys; others do different tasks.
Final Answer:
array_keys() -> Option C
Quick Check:
Keys extraction = array_keys() [OK]
Hint: Keys come from array_keys(), values from array_values() [OK]
Common Mistakes:
Confusing array_keys() with array_values()
Using array_map() to get keys
Trying to get keys with array_filter()
2. Which of the following is the correct syntax to get all values from an array $arr in PHP?
easy
A. array_values($arr);
B. array_keys($arr);
C. values_array($arr);
D. get_values($arr);
Solution
Step 1: Identify the correct function name
The function to get values is array_values().
Step 2: Check syntax correctness
Correct syntax is array_values($arr); with parentheses and variable inside.
Final Answer:
array_values($arr); -> Option A
Quick Check:
Values extraction = array_values() [OK]
Hint: Values come from array_values(), keys from array_keys() [OK]
Common Mistakes:
Using array_keys() to get values
Using non-existent functions like values_array()
Missing parentheses in function call
3. What will be the output of the following PHP code?
A. array_values() should be array_keys() to get keys
B. Missing semicolon after array declaration
C. print_r() cannot print arrays
D. Variable $keys is not defined
Solution
Step 1: Understand the goal
The code tries to get keys but uses array_values() which returns values.
Step 2: Correct function usage
To get keys, use array_keys() instead of array_values().
Final Answer:
array_values() should be array_keys() to get keys -> Option A
Quick Check:
Keys need array_keys(), not array_values() [OK]
Hint: Use array_keys() for keys, array_values() for values [OK]
Common Mistakes:
Mixing up array_keys() and array_values()
Assuming print_r() can't print arrays
Ignoring correct variable definitions
5. Given the array $data = ['apple' => 3, 'banana' => 0, 'cherry' => 5, 'date' => 0];, which PHP code correctly extracts only the keys with non-zero values?
hard
A. $keys = array_keys($data);
B. $keys = array_keys(array_filter($data));
C. $keys = array_values(array_filter($data));
D. $keys = array_filter(array_keys($data));
Solution
Step 1: Filter array to keep non-zero values
array_filter($data) removes entries with zero values.
Step 2: Extract keys from filtered array
array_keys() gets keys from the filtered array, giving only keys with non-zero values.
Final Answer:
$keys = array_keys(array_filter($data)); -> Option B
Quick Check:
Filter then keys = array_keys(array_filter()) [OK]
Hint: Filter first, then get keys with array_keys(array_filter()) [OK]
Common Mistakes:
Using array_values() instead of array_keys() after filtering
Not filtering before extracting keys
Filtering keys instead of values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.