Bird
0
0

Consider this PHP code snippet:

medium📝 Debug Q14 of 15
PHP - Array Functions
Consider this PHP code snippet:
$arr = ['x' => 'dog', 'y' => 'cat', 'z' => 'dog'];
$result = array_flip($arr);
print_r($result);
What is the problem with this code?
ANo problem; output will be correct
BIt causes a syntax error because array_flip requires numeric keys
Carray_flip cannot flip string keys
DThe duplicate values cause keys to be overwritten in the flipped array
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_flip() behavior with duplicates

    When flipping, values become keys. Duplicate values cause keys to overwrite previous ones.
  2. Step 2: Analyze the example

    'dog' appears twice as a value. After flipping, only the last key ('z') remains for 'dog'.
  3. Final Answer:

    The duplicate values cause keys to be overwritten in the flipped array -> Option D
  4. Quick Check:

    Duplicate values overwrite keys when flipped [OK]
Quick Trick: Duplicates in values overwrite keys after flip [OK]
Common Mistakes:
  • Expecting all keys to be preserved
  • Thinking array_flip causes syntax errors
  • Assuming string keys cannot be flipped

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes