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.