Bird
Raised Fist0
PHPprogramming~20 mins

Array access and modification in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code?

Consider the following PHP code snippet:

$arr = [1, 2, 3, 4];
$arr[2] = $arr[0] + $arr[1];
echo $arr[2];

What will be printed?

PHP
$arr = [1, 2, 3, 4];
$arr[2] = $arr[0] + $arr[1];
echo $arr[2];
A5
B3
C6
DUndefined offset error
Attempts:
2 left
💡 Hint

Remember that array indices start at 0 and you can assign new values to existing keys.

❓ Predict Output
intermediate
2:00remaining
What is the output after modifying a nested array?

Look at this PHP code:

$arr = ["a" => [1, 2], "b" => [3, 4]];
$arr["a"][1] = 5;
echo $arr["a"][1];

What will it print?

PHP
$arr = ["a" => [1, 2], "b" => [3, 4]];
$arr["a"][1] = 5;
echo $arr["a"][1];
A5
B2
C1
DUndefined index error
Attempts:
2 left
💡 Hint

Think about how to access and change values inside nested arrays.

🔧 Debug
advanced
2:00remaining
Identify the error in this array modification code

What error does this PHP code produce?

$arr = [10, 20, 30];
$arr[3] += 5;
echo $arr[3];
PHP
$arr = [10, 20, 30];
$arr[3] += 5;
echo $arr[3];
AOutputs 5
BNotice: Undefined offset: 3
CFatal error: Cannot use undefined offset
DOutputs 0
Attempts:
2 left
💡 Hint

Think about what happens when you try to add to an array element that does not exist yet.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in array modification?

Which of the following PHP code snippets will cause a syntax error?

A
$arr = [1, 2, 3];
$arr[0] = $arr[0] + 1;
B
$arr = [1, 2, 3];
$arr[] = 4;
C
$arr = [1, 2, 3];
$arr[2] +=;
D
$arr = [1, 2, 3];
$arr[1] = 5;
Attempts:
2 left
💡 Hint

Look carefully at the assignment operators and their usage.

🚀 Application
expert
3:00remaining
How many elements are in the array after these modifications?

Given this PHP code:

$arr = [];
$arr[2] = 'a';
$arr[] = 'b';
$arr[5] = 'c';
$arr[] = 'd';

How many elements does $arr have after these lines run?

PHP
$arr = [];
$arr[2] = 'a';
$arr[] = 'b';
$arr[5] = 'c';
$arr[] = 'd';
A6
B3
C5
D4
Attempts:
2 left
💡 Hint

Remember how PHP assigns keys when using empty brackets and how explicit keys affect the array.

Practice

(1/5)
1. What is the correct way to access the first element of an array $arr in PHP?
easy
A. $arr{0}
B. $arr[1]
C. $arr[0]
D. $arr->0

Solution

  1. Step 1: Understand PHP array indexing

    PHP arrays start at index 0, so the first element is at index 0.
  2. Step 2: Identify correct syntax for array access

    Use square brackets with the index number: $arr[0].
  3. Final Answer:

    $arr[0] -> Option C
  4. Quick Check:

    First element index = 0 [OK]
Hint: First element is always at index 0 in PHP arrays [OK]
Common Mistakes:
  • Using index 1 for first element
  • Using curly braces for array access
  • Using object property syntax
2. Which of the following is the correct way to change the value of the third element in array $arr to 10?
easy
A. $arr(3) = 10;
B. $arr[2] = 10;
C. $arr->2 = 10;
D. $arr{3} = 10;

Solution

  1. Step 1: Identify the index of the third element

    Arrays start at 0, so the third element is at index 2.
  2. Step 2: Use correct syntax to assign value

    Use square brackets with index 2: $arr[2] = 10;.
  3. Final Answer:

    $arr[2] = 10; -> Option B
  4. Quick Check:

    Index 2 for third element assignment [OK]
Hint: Use square brackets and zero-based index to modify array [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using object property syntax
  • Using wrong index number
3. What will be the output of the following PHP code?
$arr = [5, 10, 15];
$arr[1] = $arr[1] + 5;
echo $arr[1];
medium
A. 15
B. 10
C. 20
D. 5

Solution

  1. Step 1: Understand initial array values

    $arr = [5, 10, 15]; so $arr[1] is 10.
  2. Step 2: Calculate new value of $arr[1]

    $arr[1] = $arr[1] + 5 means 10 + 5 = 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    10 + 5 = 15 [OK]
Hint: Add to existing value using $arr[index] + number [OK]
Common Mistakes:
  • Printing old value without update
  • Adding index instead of value
  • Confusing array indexes
4. Identify the error in the following PHP code snippet:
$arr = [1, 2, 3];
$arr[3] = 4;
echo $arr[3];
medium
A. Accessing an undefined index causes an error
B. Cannot echo array elements directly
C. Syntax error in array declaration
D. Assigning to index 3 adds a new element without error

Solution

  1. Step 1: Check array indexes and assignment

    Array has indexes 0,1,2 initially. Assigning $arr[3] = 4 adds a new element at index 3.
  2. Step 2: Confirm if this causes error

    PHP allows adding new elements by assigning to new indexes without error.
  3. Final Answer:

    Assigning to index 3 adds a new element without error -> Option D
  4. Quick Check:

    PHP arrays auto-expand on new index assignment [OK]
Hint: Assigning new index adds element; no error in PHP [OK]
Common Mistakes:
  • Thinking accessing undefined index causes error
  • Confusing syntax error with runtime behavior
  • Believing echo can't print array elements
5. Given the array $arr = [2, 4, 6, 8];, which code snippet correctly doubles each element's value by modifying the array in place?
hard
A. for ($i = 0; $i < count($arr); $i++) { $arr[$i] *= 2; }
B. $arr = array_fill(0, count($arr), 2 * $arr[0]);
C. array_map(fn($v) => $v * 2, $arr);
D. foreach ($arr as $value) { $value = $value * 2; }

Solution

  1. Step 1: Understand how to modify array elements in place

    Using a for loop with index allows direct modification of each element.
  2. Step 2: Analyze each option

    A: foreach copies values, does not modify original array.
    B: array_fill replaces all elements with same value, incorrect.
    C: array_map returns new array, original unchanged.
    D: for loop modifies $arr[$i] directly.
  3. Final Answer:

    for ($i = 0; $i < count($arr); $i++) { $arr[$i] *= 2; } -> Option A
  4. Quick Check:

    Use indexed loop to modify array elements [OK]
Hint: Use for loop with indexes to change array elements directly [OK]
Common Mistakes:
  • Modifying foreach loop variable instead of array
  • Expecting array_map to change original array
  • Using array_fill incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard