codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

20 / 56 topics
19Arrays in PHP20Indexed Arrays21Associative Arrays22Multidimensional Arrays23Array Functions
Tutorials/PHP/Indexed Arrays
🐘PHP

Indexed Arrays

Updated 2026-05-15
10 min read

Indexed Arrays

Introduction

In PHP, arrays are a fundamental data structure that can store multiple values in a single variable. There are two main types of arrays in PHP: indexed arrays and associative arrays. This tutorial will focus on indexed arrays, which use numeric keys.

Indexed arrays are the simplest type of array where each element is assigned a numeric key starting from 0. They are similar to lists or vectors found in other programming languages. Understanding indexed arrays is crucial for handling collections of data efficiently in PHP.

Concept

An indexed array in PHP is an ordered collection of values, where each value has a corresponding numeric index. The first element has an index of 0, the second element has an index of 1, and so on. This numeric indexing allows you to access elements directly using their position in the array.

Indexed arrays are particularly useful when you need to store a list of items that do not require named keys. They provide fast access to elements based on their position and are ideal for scenarios where order matters.

Examples

Let's explore some practical examples to illustrate how indexed arrays work in PHP.

Creating an Indexed Array

You can create an indexed array using the array() function or the shorthand syntax [].

Using array() Function

$fruits = array("Apple", "Banana", "Cherry");

Using Shorthand Syntax

$fruits = ["Apple", "Banana", "Cherry"];

Both methods create an indexed array with three elements: "Apple" at index 0, "Banana" at index 1, and "Cherry" at index 2.

Accessing Elements

You can access elements in an indexed array using their numeric keys.

echo $fruits[0]; // Outputs: Apple
echo $fruits[1]; // Outputs: Banana

Adding Elements

To add elements to an indexed array, you can use the [] operator or specify a new index directly.

Using [] Operator

$fruits[] = "Date"; // Adds "Date" at the end of the array

Specifying Index

$fruits[3] = "Elderberry"; // Adds "Elderberry" at index 3

Modifying Elements

You can modify elements in an indexed array by assigning a new value to their existing index.

$fruits[1] = "Blueberry"; // Changes "Banana" to "Blueberry"

Looping Through Indexed Arrays

Indexed arrays are often used in loops to iterate over their elements. PHP provides several ways to loop through arrays, such as foreach, for, and while.

Using foreach

foreach ($fruits as $fruit) {
    echo $fruit . "\n";
}

<OutputBlock>
{`Apple  
Banana  
Cherry  
Date  
Elderberry`}
</OutputBlock>

#### Using `for`

```php
for ($i = 0; $i < count($fruits); $i++) {
    echo $fruits[$i] . "\n";
}

<OutputBlock>
{`Apple  
Banana  
Cherry  
Date  
Elderberry`}
</OutputBlock>

### Removing Elements

To remove elements from an indexed array, you can use the `unset()` function.

```php
unset($fruits[1]); // Removes "Blueberry" (original "Banana")

After this operation, the array will look like this:

print_r($fruits);
Output
Array  
(  
  [0] => Apple  
  [2] => Cherry  
  [3] => Date  
  [4] => Elderberry  
)

Notice that the keys are not re-indexed. If you want to re-index the array, you can use array_values().

$fruits = array_values($fruits);
print_r($fruits);
Output
Array  
(  
  [0] => Apple  
  [1] => Cherry  
  [2] => Date  
  [3] => Elderberry  
)

What's Next?

Now that you have a solid understanding of indexed arrays, the next step is to explore associative arrays. Associative arrays allow you to assign custom keys to each element, providing more flexibility and readability in your code.

Stay tuned for the next tutorial where we will dive into associative arrays and how they differ from indexed arrays.


PreviousArrays in PHPNext Associative Arrays

Recommended Gear

Arrays in PHPAssociative Arrays