Bird
0
0

You want to create an associative array from two indexed arrays:

hard📝 Application Q8 of 15
PHP - Arrays
You want to create an associative array from two indexed arrays:
$keys = ["id", "name", "age"];
$values = [101, "Bob", 25];
// Create associative array $arr

Which code correctly creates $arr?
A$arr = array_merge($keys, $values);
B$arr = array($keys => $values);
C$arr = [$keys => $values];
D$arr = array_combine($keys, $values);
Step-by-Step Solution
Solution:
  1. Step 1: Understand array_combine function

    array_combine creates associative array from keys and values arrays.
  2. Step 2: Check each option for correct usage

    $arr = array_combine($keys, $values); correctly uses array_combine($keys, $values).
  3. Final Answer:

    $arr = array_combine($keys, $values); -> Option D
  4. Quick Check:

    Use array_combine to pair keys and values [OK]
Quick Trick: Use array_combine(keys, values) to create associative arrays [OK]
Common Mistakes:
  • Using array() with arrays as keys
  • Using array_merge instead of combine
  • Wrong syntax for pairing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes