Arrays hold many values in one place. Accessing and changing these values helps you work with data easily.
Array access and modification in PHP
<?php // Define an array $array = ["apple", "banana", "cherry"]; // Access an element by index $value = $array[1]; // Modify an element by index $array[1] = "blueberry"; ?>
Array indexes start at 0, so $array[0] is the first item.
You can change the value by assigning a new value to the index.
<?php // Access first element $array = [10, 20, 30]; echo $array[0]; ?>
<?php // Modify last element $array = ["red", "green", "blue"]; $array[2] = "yellow"; echo $array[2]; ?>
<?php // Access empty array $array = []; // echo $array[0]; // This will cause an error because the array is empty ?>
<?php // Modify single element array $array = [100]; $array[0] = 200; echo $array[0]; ?>
This program shows how to access and change an item in an array. It prints the array before and after the change.
<?php // Create an array of fruits $fruits = ["apple", "banana", "cherry"]; // Print original array echo "Original array:\n"; print_r($fruits); // Access the second fruit $second_fruit = $fruits[1]; echo "\nSecond fruit: $second_fruit\n"; // Modify the second fruit $fruits[1] = "blueberry"; echo "\nModified array:\n"; print_r($fruits); ?>
Accessing or modifying an element by index is very fast, usually O(1) time.
Be careful not to access an index that does not exist; it causes errors or warnings.
Use modification when you want to update a value; use array functions if you want to add or remove elements.
Arrays store multiple values accessible by index starting at 0.
You can get or change values by using the index inside square brackets.
Always check the array size before accessing to avoid errors.
Practice
$arr in PHP?Solution
Step 1: Understand PHP array indexing
PHP arrays start at index 0, so the first element is at index 0.Step 2: Identify correct syntax for array access
Use square brackets with the index number:$arr[0].Final Answer:
$arr[0] -> Option CQuick Check:
First element index = 0 [OK]
- Using index 1 for first element
- Using curly braces for array access
- Using object property syntax
$arr to 10?Solution
Step 1: Identify the index of the third element
Arrays start at 0, so the third element is at index 2.Step 2: Use correct syntax to assign value
Use square brackets with index 2:$arr[2] = 10;.Final Answer:
$arr[2] = 10; -> Option BQuick Check:
Index 2 for third element assignment [OK]
- Using parentheses instead of brackets
- Using object property syntax
- Using wrong index number
$arr = [5, 10, 15];
$arr[1] = $arr[1] + 5;
echo $arr[1];
Solution
Step 1: Understand initial array values
$arr = [5, 10, 15]; so $arr[1] is 10.Step 2: Calculate new value of $arr[1]
$arr[1] = $arr[1] + 5 means 10 + 5 = 15.Final Answer:
15 -> Option AQuick Check:
10 + 5 = 15 [OK]
- Printing old value without update
- Adding index instead of value
- Confusing array indexes
$arr = [1, 2, 3];
$arr[3] = 4;
echo $arr[3];
Solution
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.Step 2: Confirm if this causes error
PHP allows adding new elements by assigning to new indexes without error.Final Answer:
Assigning to index 3 adds a new element without error -> Option DQuick Check:
PHP arrays auto-expand on new index assignment [OK]
- Thinking accessing undefined index causes error
- Confusing syntax error with runtime behavior
- Believing echo can't print array elements
$arr = [2, 4, 6, 8];, which code snippet correctly doubles each element's value by modifying the array in place?Solution
Step 1: Understand how to modify array elements in place
Using a for loop with index allows direct modification of each element.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.Final Answer:
for ($i = 0; $i < count($arr); $i++) { $arr[$i] *= 2; } -> Option AQuick Check:
Use indexed loop to modify array elements [OK]
- Modifying foreach loop variable instead of array
- Expecting array_map to change original array
- Using array_fill incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
