Bird
0
0

Given the array $items = ['pen' => 3, 'pencil' => 5, 'eraser' => 3];, which code snippet finds all keys with the value 3?

hard📝 Application Q8 of 15
PHP - Arrays
Given the array $items = ['pen' => 3, 'pencil' => 5, 'eraser' => 3];, which code snippet finds all keys with the value 3?
A$keys = array_search(3, $items);
B$keys = array_keys($items, 3);
C$keys = in_array(3, $items);
D$keys = array_key_exists(3, $items);
Step-by-Step Solution
Solution:
  1. Step 1: Identify function to find all keys by value

    array_keys() with second parameter returns all keys matching that value.
  2. Step 2: Compare options

    array_search() returns first key only, in_array() returns boolean, array_key_exists() checks keys.
  3. Final Answer:

    $keys = array_keys($items, 3); -> Option B
  4. Quick Check:

    Find all keys by value = array_keys() with value param [OK]
Quick Trick: Use array_keys(array, value) to get all matching keys [OK]
Common Mistakes:
  • Using array_search() for all keys
  • Using in_array() expecting keys
  • Confusing array_key_exists() with value search

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes