📦 PHP Arrays
Estimated reading: 3 minutes 268 views

PHP Indexed Array – Store and Access Ordered Data in PHP

A complete guide to creating, accessing, and working with indexed arrays in PHP.


Introduction – What Is an Indexed Array?

An indexed array in PHP is a type of array where elements are assigned numeric keys starting from 0 by default. It’s perfect for storing ordered lists of items such as colors, numbers, or product names.

In this guide, you’ll learn:

  • How to create and access indexed arrays
  • Looping techniques for indexed arrays
  • Common operations like sorting and counting
  • Real-world usage and best practices

Creating Indexed Arrays

Using Square Bracket Syntax (Preferred)

$colors = ["red", "green", "blue"];

Using array() Function (Legacy)

$colors = array("red", "green", "blue");

Accessing Indexed Array Elements

$colors = ["red", "green", "blue"];

echo $colors[0]; // red
echo $colors[1]; // green

Array indexes start at 0 and increase by one for each element.


Looping Through Indexed Arrays

Using foreach Loop

$fruits = ["apple", "banana", "cherry"];

foreach ($fruits as $fruit) {
    echo $fruit . "<br>";
}

Using for Loop

$fruits = ["apple", "banana", "cherry"];

for ($i = 0; $i < count($fruits); $i++) {
    echo $fruits[$i] . "<br>";
}

Use foreach for cleaner syntax unless you need the index.


Add, Update, and Remove Elements

Add New Item

$colors[] = "yellow"; // adds to the end

Update Existing Item

$colors[1] = "lime"; // replaces "green" with "lime"

Remove an Item

unset($colors[2]); // removes "blue"

Useful Indexed Array Functions

FunctionDescriptionExample
count()Returns number of itemscount($colors)
array_push()Adds one or more elementsarray_push($colors, "pink")
array_pop()Removes the last elementarray_pop($colors)
sort()Sorts array in ascending ordersort($colors)
rsort()Sorts array in descending orderrsort($colors)
in_array()Checks if value exists in arrayin_array("red", $colors)

Indexed vs Associative Arrays

FeatureIndexed ArrayAssociative Array
KeysNumeric (0,1,2,…)String-based (“name”, etc.)
Use CaseOrdered collectionsKey-value pair structures

Best Practices

  • Use [] syntax for simplicity
  • Loop with foreach unless you need the index
  • Don’t mix associative keys in indexed arrays
  • Use count() to avoid hardcoding loop limits

Summary – Recap & Next Steps

PHP indexed arrays are the simplest way to handle ordered collections of data. They’re widely used for lists, dropdowns, menu items, and more.

Key Takeaways:

  • Indexed arrays use numeric keys starting from 0
  • Use [] or array() to define them
  • Easily loop through using foreach or for
  • PHP provides many helpful array functions for manipulation

Real-World Use Cases:
Navigation menus, ordered product lists, color swatches, and dropdown fields.


Frequently Asked Questions (FAQs)

What is the default index of a PHP array?
If not specified, it starts from 0 and increments by 1.

Can I skip index numbers in an indexed array?
Yes, but doing so turns it into a non-continuous indexed array.

How to check if a value exists in an indexed array?
Use in_array("value", $array);

Can I mix keys in an indexed array?
Technically yes, but it’s not recommended for clarity and consistency.


Share Now :
Share

🔢 PHP Indexed Array

Or Copy Link

CONTENTS
Scroll to Top