Bird
0
0

What is the problem with this PHP code snippet?

medium📝 Debug Q6 of 15
PHP - Arrays
What is the problem with this PHP code snippet?
$keys = ['id', 'name'];
$values = ['101'];
$result = array_combine($keys, $values);
print_r($result);
AThe keys array contains invalid characters
BThe arrays have different lengths, causing array_combine to fail
Carray_combine requires numeric keys only
DThe values array must be associative
Step-by-Step Solution
Solution:
  1. Step 1: Check array_combine requirements

    array_combine requires both arrays to have the same number of elements.
  2. Step 2: Analyze given arrays

    $keys has 2 elements, $values has 1 element, so the function will fail.
  3. Final Answer:

    The arrays have different lengths, causing array_combine to fail -> Option B
  4. Quick Check:

    array_combine needs equal length arrays [OK]
Quick Trick: array_combine fails if array sizes differ [OK]
Common Mistakes:
  • Ignoring array length mismatch
  • Assuming keys must be numeric
  • Thinking values must be associative

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes