Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Array Functions
What will be the output of this PHP code?
$arr = ['a' => 1, 'b' => 2, 'c' => 1];
$flipped = array_flip($arr);
print_r($flipped);
AArray ( [1] => a [2] => b )
BArray ( [1] => c [2] => b )
CArray ( [a] => 1 [b] => 2 [c] => 1 )
DArray ( [1] => a [2] => c )
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_flip() behavior with duplicate values

    array_flip() swaps keys and values. When values are duplicated (1 appears twice), the last key ('c') overwrites the first ('a').
  2. Step 2: Determine final flipped array

    The flipped array has keys 1 and 2 with values 'c' and 'b' respectively.
  3. Final Answer:

    Array ( [1] => c [2] => b ) -> Option B
  4. Quick Check:

    array_flip() overwrites duplicate keys, last wins [OK]
Quick Trick: array_flip() keeps last key for duplicate values [OK]
Common Mistakes:
  • Expecting first key to be kept for duplicates
  • Thinking array_flip() preserves all duplicates
  • Confusing keys and values after flip

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes