← Back to PHP Course | Chapter 7: Arrays | Lesson 7 of 13

PHP Indexed Arrays

An indexed array is a numbered list, like a queue where each person has a spot number starting at zero. You get an item by asking for its number.
Syntax
php
$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
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print `$fruits[0]` to the output
echo $fruits[0];
?>

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
<?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);
?>

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
<?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);
?>

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
<?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";
}
?>

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
<?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";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Starting to count at 1, so $arr[1] skips the first element, when PHP indexes begin at 0.
  2. Using unset($arr[1]) and expecting the remaining indexes to shift down, when the keys stay as they were.
  3. Adding an element with $arr[5] = x and expecting it at the end, when it uses key 5 exactly.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.