PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Indexed arrays in PHP are a type of array where each element is assigned a numerical index automatically or manually. PHP arrays are flexible and can hold various data types, such as strings, integers, floats, objects, and even other arrays. Below is a detailed description of indexed arrays in PHP, including how to create, access, and manipulate them.

Creating Indexed Arrays

You can create indexed arrays in PHP in multiple ways:

1. Using the 'array()' function

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

				
			

2. Using square brackets

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

				
			

Accessing Array Elements

You can access elements in an indexed array by referring to their index number. The index of the first element is 0.

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

				
			

Adding Elements to an Indexed Array

You can add elements to an indexed array by specifying the index or using the ‘[]‘ operator to add elements to the end of the array.

1. Specifying the index

				
					$fruits[3] = "Date";

				
			

2. Using the '[ ]' operator

				
					$fruits[] = "Elderberry";

				
			

Looping Through Indexed Arrays

You can loop through indexed arrays using ‘foreach‘ or ‘for‘ loops.

Using 'foreach'

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

				
			

Using 'for'

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

				
			

Modifying Elements

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

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

				
			

Removing Elements

You can remove elements from an indexed array using the ‘unset()‘ function. Note that this will not reindex the array.

				
					unset($fruits[1]); // Removes the element at index 1 ("Banana")

				
			

To reindex the array, you can use the ‘array_values()‘ function.

				
					$fruits = array_values($fruits);

				
			

Array Functions

PHP provides many built-in functions to work with arrays. Some common functions include:

'count()'

Returns the number of elements in an array.

				
					echo count($fruits); // Outputs the number of elements in $fruits

				
			

'array_push()'

Adds one or more elements to the end of an array.

				
					array_push($fruits, "Fig", "Grape");

				
			

'array_pop()'

Removes the last element of an array.

				
					array_pop($fruits);

				
			

array_merge()

Merges one or more arrays.

				
					$more_fruits = array("Honeydew", "Jackfruit");
$all_fruits = array_merge($fruits, $more_fruits);

				
			

Example

Here is a comprehensive example demonstrating various operations with indexed arrays:

				
					$fruits = ["Apple", "Banana", "Cherry"]; // Create an array

$fruits[] = "Date"; // Add element to the end
$fruits[4] = "Elderberry"; // Add element at specific index

echo $fruits[0]; // Access an element

$fruits[1] = "Blueberry"; // Modify an element

unset($fruits[2]); // Remove an element

foreach ($fruits as $fruit) { // Loop through the array
    echo $fruit . "<br>";
}

echo count($fruits); // Get the number of elements

array_push($fruits, "Fig", "Grape"); // Add multiple elements
array_pop($fruits); // Remove the last element

$more_fruits = ["Honeydew", "Jackfruit"];
$all_fruits = array_merge($fruits, $more_fruits); // Merge arrays

print_r($all_fruits); // Print the array structure

				
			

Indexed arrays in PHP are powerful and flexible, allowing you to store, access, and manipulate collections of data easily. Understanding how to use them effectively is essential for any PHP developer.

Scroll to Top