Bird
0
0

You have an array $data = [10, 20, 30, 40, 50, 60]. Using array_splice, how do you replace elements 20, 30, and 40 with 21 and 31?

hard📝 Application Q8 of 15
PHP - Array Functions
You have an array $data = [10, 20, 30, 40, 50, 60]. Using array_splice, how do you replace elements 20, 30, and 40 with 21 and 31?
Aarray_splice($data, 2, 3, [21, 31]);
Barray_splice($data, 1, 3, [21, 31]);
Carray_splice($data, 1, 2, [21, 31]);
Darray_splice($data, 0, 3, [21, 31]);
Step-by-Step Solution
Solution:
  1. Step 1: Identify indexes of elements to replace

    Elements 20, 30, 40 are at indexes 1, 2, and 3.
  2. Step 2: Use array_splice to replace 3 elements starting at index 1

    array_splice($data, 1, 3, [21, 31]) removes 3 elements and inserts 21 and 31.
  3. Final Answer:

    array_splice($data, 1, 3, [21, 31]); -> Option B
  4. Quick Check:

    Replace 3 elements at index 1 with 2 elements = array_splice($data, 1, 3, [21, 31]); [OK]
Quick Trick: array_splice(start, length, new_array) replaces elements [OK]
Common Mistakes:
  • Wrong start index
  • Wrong length parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes