Bird
0
0

What is the output of the following PHP code?

medium📝 Predict Output Q4 of 15
PHP - Array Functions
What is the output of the following PHP code?
$arr = ['x' => 'apple', 'y' => 'banana', 'z' => 'apple'];
$result = array_unique($arr);
print_r($result);
AArray ( [x] => apple [y] => banana [z] => apple )
BArray ( [x] => apple [y] => banana )
CArray ( [y] => banana [z] => apple )
DArray ( [x] => apple [z] => apple )
Step-by-Step Solution
Solution:
  1. Step 1: Apply array_unique() to the array

    The function removes duplicate values, keeping the first occurrence of 'apple' at key 'x' and 'banana' at 'y'. The second 'apple' at 'z' is removed.
  2. Step 2: Understand the resulting array

    The resulting array has keys 'x' and 'y' with values 'apple' and 'banana' respectively.
  3. Final Answer:

    Array ( [x] => apple [y] => banana ) -> Option B
  4. Quick Check:

    array_unique() removes duplicates, keeps first occurrence [OK]
Quick Trick: array_unique() keeps first unique value, removes later duplicates [OK]
Common Mistakes:
  • Expecting all duplicates to remain
  • Thinking keys are reindexed
  • Assuming last duplicate is kept

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes