Lesson 22 of 50 – PHP Indexed Arrays
44%

PHP Indexed Arrays

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.

1. What Is an Indexed Array?

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);
?>
Output:
Array
(
    [0] => Rahul
    [1] => Amit
    [2] => Priya
)

PHP automatically assigns keys starting at 0 when values are added without explicitly specifying keys.

2. Creating Indexed Arrays

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);
?>
Output for both examples:
Array
(
    [0] => Red
    [1] => Green
    [2] => Blue
)

Both syntaxes work. Square brackets are commonly preferred in modern PHP code.

3. Accessing Array Elements

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];
?>
Output:
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];
?>
Output:
200
4. Adding Elements to an Indexed Array

Use $array[] = $value to append a new element to an indexed array.

<?php
$fruits = ["Apple", "Mango"];

$fruits[] = "Banana";
$fruits[] = "Orange";

print_r($fruits);
?>
Output:
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);
?>
Output:
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.

5. Updating and Removing Elements

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);
?>
Output:
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);
?>
Output:
Array
(
    [0] => Green
    [1] => Blue
)

The array_values() function returns the values with new consecutive numeric indexes.

6. Counting Indexed Array Elements

Use count() to find the number of top-level elements in an array.

<?php
$students = ["Rahul", "Amit", "Priya", "Neha"];

echo "Total Students: " . count($students);
?>
Output:
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>";
}
?>
Output:
10
20
30
40
50

This loop works because the array uses consecutive numeric indexes starting at 0.

7. Displaying Indexed Arrays with foreach

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>";
}
?>
Output:
English
Hindi
Math
Computer

Display both index and value:

<?php
$subjects = ["English", "Hindi", "Math"];

foreach ($subjects as $index => $subject) {
    echo $index . ": " . $subject . "<br>";
}
?>
Output:
0: English
1: Hindi
2: Math

This approach is especially useful when you need both the numeric key and the value.

8. Searching and Sorting Indexed Arrays

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";
}
?>
Output:
Mango found

Sort values in ascending order:

<?php
$numbers = [40, 10, 30, 20];

sort($numbers);

print_r($numbers);
?>
Output:
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);
?>
Output:
Array
(
    [0] => 40
    [1] => 30
    [2] => 20
    [3] => 10
)

Both sort() and rsort() reindex the array with consecutive numeric keys.

9. Practical Example: Student Marks

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;
?>
Output:
Student Marks
Total: 400
Subjects: 5
Average: 80

The array_sum() function adds the numeric values, while count() returns the number of elements.

Practice Assignments
  1. Create an indexed array containing five city names.
  2. Display the first, third, and fifth elements.
  3. Add two new elements to an existing array.
  4. Change the value at index 2.
  5. Remove an element using unset() and reindex the array.
  6. Display all values using foreach.
  7. Sort an array of ten numbers in ascending order.
  8. Calculate the total and average of five student marks.

🧠 Quick Quiz

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?