Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Arrays
What will be the output of this PHP code?
$keys = ['a', 'b'];
$values = [1, 2];
$result = array_combine($keys, $values);
print_r($result);
AArray ( [0] => a [1] => b [2] => 1 [3] => 2 )
BArray ( [a] => 1 [b] => 2 )
CArray ( [1] => a [2] => b )
DError: keys and values must be same length
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_combine usage

    It creates an associative array using first array as keys and second as values.
  2. Step 2: Check input arrays

    Both arrays have same length, so combination is valid.
  3. Final Answer:

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

    array_combine output = Associative array from keys and values [OK]
Quick Trick: array_combine needs equal length arrays [OK]
Common Mistakes:
  • Mixing up keys and values order
  • Expecting merged numeric keys
  • Ignoring length mismatch errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes