PHP Indexed Arrays
In this page:
$array = [value1, value2, value3]; // keys 0, 1, 2
$array[0]; // read
$array[0] = new_value; // overwrite
$array[] = value; // append
Creating Indexed Arrays
An indexed array uses automatically assigned, sequential integer keys starting at 0, so $fruits[0] refers to the first element you added, exactly like arrays in most other programming languages.
Example: Creating Indexed Arrays
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print `$fruits[0]` to the output
echo $fruits[0];
?>
Login to try C/C++/Java/PHP code in the editor
Accessing Array Elements
You can access or overwrite any element by its numeric position, such as $colors[2] = blue;, and PHP will happily let you skip indices, creating gaps that count() still accounts for correctly.
Example: Accessing Array Elements
<?php
// Declare `$colors` as an array: `["red", "green", "yellow"]`
$colors = ["red", "green", "yellow"];
// Set `$colors[2]` to 'blue'
$colors[2] = 'blue';
// Print a human-readable dump of `$colors`
print_r($colors);
?>
Login to try C/C++/Java/PHP code in the editor
Adding Elements to Arrays
$fruits[] = mango; appends a new value to the end of an indexed array, automatically assigning it the next available integer key without you needing to track the current highest index yourself.
Example: Adding Elements to Arrays
<?php
// Declare `$fruits` as an array: `["apple", "banana"]`
$fruits = ["apple", "banana"];
// Append 'mango' to the end of `$fruits`
$fruits[] = 'mango';
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
Removing Array Elements
foreach ($array as $value) is the idiomatic way to loop over an indexed array's values in order, and is generally preferred over a manual for loop with a counter since it can't accidentally run past the array's bounds.
Example: Removing Array Elements
<?php
// Declare `$fruits` as an array: `["apple", "banana", "mango"]`
$fruits = ["apple", "banana", "mango"];
// Loop over `$fruits`, binding each item to `$fruit`
foreach ($fruits as $fruit) {
// Print `$fruit . "\n"` to the output
echo $fruit . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Modifying and Reindexing
Indexed arrays are the natural fit for ordered lists like a queue of tasks or a sequence of steps, where the position of an item (first, second, third) carries meaning rather than a descriptive label.
Example: Modifying and Reindexing
<?php
// Declare `$tasks` as an array: `["Write report", "Send email", "Attend meeting"]`
$tasks = ["Write report", "Send email", "Attend meeting"];
// Loop over `$tasks`, binding each item to `$index => $task`
foreach ($tasks as $index => $task) {
// Print `($index + 1) . ". " . $task . "\n"` to the output
echo ($index + 1) . ". " . $task . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
- Starting to count at 1, so
$arr[1]skips the first element, when PHP indexes begin at 0. - Using
unset($arr[1])and expecting the remaining indexes to shift down, when the keys stay as they were. - Adding an element with
$arr[5] = xand expecting it at the end, when it uses key 5 exactly.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: