PHP Indexed Arrays (Step-by-Step Guide with Examples)

PHP Indexed Arrays are arrays with numeric indexes that start from 0 by default. These arrays allow you to store multiple values under a single variable, making it easier to manage related data.

1. What is an Indexed Array?

An indexed array in PHP is an array where each element is assigned a numeric index starting from 0.

Example: Creating an Indexed Array

<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0]; // Output: Apple
?>

2. Creating an Indexed Array

Method 1: Using Square Brackets ([])

<?php
$colors = ["Red", "Green", "Blue"];
?>

Method 2: Using array() Function

<?php
$colors = array("Red", "Green", "Blue");
?>

3. Accessing Elements in an Indexed Array

You can access elements using their index. The first element starts at index 0.

<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[1]; // Output: Green
?>

4. Adding Elements to an Indexed Array

Adding Elements Manually

 
<?php
$colors = ["Red", "Green"];
$colors[] = "Blue"; // Adds "Blue" to the array
print_r($colors);
?>

Using array_push()

<?php
$colors = ["Red", "Green"];
array_push($colors, "Blue", "Yellow"); // Adds multiple elements
print_r($colors);
?>

5. Modifying Elements in an Indexed Array

You can change an element by accessing it via its index.

 
<?php
$colors = ["Red", "Green", "Blue"];
$colors[1] = "Yellow"; // Changes "Green" to "Yellow"
print_r($colors);
?>

6. Removing Elements from an Indexed Array

Using unset() (Removes a Specific Element)

 
<?php
$colors = ["Red", "Green", "Blue"];
unset($colors[1]); // Removes "Green"
print_r($colors);
?>

💡 Note: The indexes will not reset automatically.

Using array_splice() (Reindexes Automatically)

 
<?php
$colors = ["Red", "Green", "Blue"];
array_splice($colors, 1, 1); // Removes "Green" and shifts elements
print_r($colors);
?>

7. Looping Through an Indexed Array

Using for Loop

<?php
$colors = ["Red", "Green", "Blue"];
for ($i = 0; $i < count($colors); $i++) {
    echo $colors[$i] . "<br>";
}
?>

8. Sorting Indexed Arrays

Sorting in Ascending Order (sort())

<?php
$numbers = [4, 2, 8, 1];
sort($numbers);
print_r($numbers); // Output: [1, 2, 4, 8]
?>

Sorting in Descending Order (rsort())

<?php
$numbers = [4, 2, 8, 1];
rsort($numbers);
print_r($numbers); // Output: [8, 4, 2, 1]
?>

9. Useful PHP Array Functions

FunctionDescriptionExample
count()Returns number of elementscount($arr)
array_push()Adds element to the endarray_push($arr, "PHP")
array_pop()Removes last elementarray_pop($arr)
array_merge()Merges two arraysarray_merge($arr1, $arr2)
in_array()Checks if a value existsin_array("PHP", $arr)
array_values()Gets all values from an arrayarray_values($arr)
sort()Sorts an array in ascending ordersort($arr)
rsort()Sorts an array in descending orderrsort($arr)

10. Converting Indexed Arrays to Strings

Convert Array to String (Implode)

 
<?php
$colors = ["Red", "Blue", "Green"];
echo implode(", ", $colors); // Output: Red, Blue, Green
?>

Convert String to Array (Explode)

<?php
$text = "PHP, JavaScript, Python";
$languages = explode(", ", $text);
print_r($languages);
?>