Bird
0
0

Given two arrays:

hard📝 Application Q15 of 15
PHP - Arrays
Given two arrays:
$keys = ['name', 'age', 'city'];
$values = ['Alice', 30, 'Paris'];
Which PHP code correctly creates an associative array and then merges it with ['country' => 'France']?
A$assoc = array_combine($keys, $values); $final = array_merge($assoc, ['country' => 'France']);
B$assoc = array_merge($keys, $values); $final = array_combine($assoc, ['country' => 'France']);
C$final = array_combine($keys, $values, ['country' => 'France']);
D$final = array_merge(array_combine($values, $keys), ['country' => 'France']);
Step-by-Step Solution
Solution:
  1. Step 1: Create associative array from keys and values

    Use array_combine($keys, $values) to pair keys with values correctly.
  2. Step 2: Merge associative array with another array

    Use array_merge to add the 'country' key-value pair to the associative array.
  3. Step 3: Check other options for errors

    $assoc = array_merge($keys, $values); $final = array_combine($assoc, ['country' => 'France']); swaps functions incorrectly, C uses wrong parameter count, D swaps keys and values.
  4. Final Answer:

    $assoc = array_combine($keys, $values); $final = array_merge($assoc, ['country' => 'France']); -> Option A
  5. Quick Check:

    Combine keys-values, then merge extra array = A [OK]
Quick Trick: Combine keys-values first, then merge extra pairs [OK]
Common Mistakes:
  • Swapping keys and values in array_combine
  • Trying to combine more than two arrays at once
  • Using array_merge before array_combine

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes