An indexed array is an array whose elements are accessed using numeric keys. In a typical indexed array, the first element has index 0, the second has index 1, and so on.
An indexed array stores multiple values under numeric keys. It is useful for storing lists such as student names, city names, and product names.
<?php
$students = ["Rahul", "Amit", "Priya"];
print_r($students);
?>
Array
(
[0] => Rahul
[1] => Amit
[2] => Priya
)
PHP automatically assigns keys starting at 0 when values are added without explicitly specifying keys.
You can create an indexed array using square brackets
or the traditional array() syntax.
Using square brackets:
<?php
$colors = ["Red", "Green", "Blue"];
print_r($colors);
?>
Using array():
<?php
$colors = array("Red", "Green", "Blue");
print_r($colors);
?>
Array
(
[0] => Red
[1] => Green
[2] => Blue
)
Both syntaxes work. Square brackets are commonly preferred in modern PHP code.
Use the element's index inside square brackets to access its value.
<?php
$fruits = ["Apple", "Mango", "Banana", "Orange"];
echo $fruits[0];
echo "<br>";
echo $fruits[1];
echo "<br>";
echo $fruits[2];
echo "<br>";
echo $fruits[3];
?>
Apple
Mango
Banana
Orange
Remember that normal indexed arrays begin at index 0, not index 1.
Access an element using a variable:
<?php
$numbers = [100, 200, 300];
$index = 1;
echo $numbers[$index];
?>
200
Use $array[] = $value to append a new
element to an indexed array.
<?php
$fruits = ["Apple", "Mango"];
$fruits[] = "Banana";
$fruits[] = "Orange";
print_r($fruits);
?>
Array
(
[0] => Apple
[1] => Mango
[2] => Banana
[3] => Orange
)
Adding an element at a specific index:
<?php
$numbers = [10, 20, 30];
$numbers[1] = 25;
print_r($numbers);
?>
Array
(
[0] => 10
[1] => 25
[2] => 30
)
Assigning to an existing index replaces its value. Assigning to a new numeric key creates an element at that key.
You can update an element by assigning a new value
to its index. Use unset() to remove
an element by key.
<?php
$colors = ["Red", "Green", "Blue"];
$colors[1] = "Yellow";
unset($colors[0]);
print_r($colors);
?>
Array
(
[1] => Yellow
[2] => Blue
)
Notice that removing index 0 does not automatically renumber the remaining keys.
Reindexing the array:
<?php
$colors = ["Red", "Green", "Blue"];
unset($colors[0]);
$colors = array_values($colors);
print_r($colors);
?>
Array
(
[0] => Green
[1] => Blue
)
The array_values() function returns
the values with new consecutive numeric indexes.
Use count() to find the number of
top-level elements in an array.
<?php
$students = ["Rahul", "Amit", "Priya", "Neha"];
echo "Total Students: " . count($students);
?>
Total Students: 4
Using count() with a for loop:
<?php
$numbers = [10, 20, 30, 40, 50];
for ($i = 0; $i < count($numbers); $i++) {
echo $numbers[$i] . "<br>";
}
?>
10
20
30
40
50
This loop works because the array uses consecutive numeric indexes starting at 0.
The foreach loop reads each array element without requiring you to manage an index manually.
<?php
$subjects = ["English", "Hindi", "Math", "Computer"];
foreach ($subjects as $subject) {
echo $subject . "<br>";
}
?>
English
Hindi
Math
Computer
Display both index and value:
<?php
$subjects = ["English", "Hindi", "Math"];
foreach ($subjects as $index => $subject) {
echo $index . ": " . $subject . "<br>";
}
?>
0: English
1: Hindi
2: Math
This approach is especially useful when you need both the numeric key and the value.
PHP provides built-in functions to search and sort indexed arrays.
Search using in_array():
<?php
$fruits = ["Apple", "Mango", "Banana"];
if (in_array("Mango", $fruits, true)) {
echo "Mango found";
} else {
echo "Mango not found";
}
?>
Mango found
Sort values in ascending order:
<?php
$numbers = [40, 10, 30, 20];
sort($numbers);
print_r($numbers);
?>
Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 40
)
Sort values in descending order:
<?php
$numbers = [40, 10, 30, 20];
rsort($numbers);
print_r($numbers);
?>
Array
(
[0] => 40
[1] => 30
[2] => 20
[3] => 10
)
Both sort() and rsort()
reindex the array with consecutive numeric keys.
We can store marks in an indexed array and calculate their total and average.
<?php
$marks = [80, 75, 90, 85, 70];
$total = array_sum($marks);
$subjects = count($marks);
$average = $total / $subjects;
echo "Student Marks<br>";
echo "Total: " . $total . "<br>";
echo "Subjects: " . $subjects . "<br>";
echo "Average: " . $average;
?>
Student Marks
Total: 400
Subjects: 5
Average: 80
The array_sum() function adds the
numeric values, while count() returns
the number of elements.
1. What is the first index of a normal indexed array?
2. Which function counts array elements?
3. Which function sorts array values in ascending order?