Introduction

Indexed arrays in PHP are arrays where each element is assigned a numeric index. These arrays are useful when you need to store a list of items and access them using their position in the list.

Key Concepts

  • Definition: An indexed array is an array with numeric indices.
  • Creation: Indexed arrays can be created using the array() function or the short array syntax [].
  • Accessing Elements: Elements in an indexed array can be accessed using their numeric index.
  • Looping Through Arrays: You can loop through indexed arrays using for or foreach loops.

Creating Indexed Arrays

Using the array() Function

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

Using the Short Array Syntax

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

Accessing Elements

You can access elements in an indexed array using their index:

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

Modifying Elements

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

$fruits[1] = "Blueberry";
echo $fruits[1]; // Outputs: Blueberry

Adding Elements

You can add elements to an indexed array by specifying a new index or using the empty brackets []:

$fruits[] = "Date";
echo $fruits[3]; // Outputs: Date

Looping Through Indexed Arrays

Using a for Loop

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

Using a foreach Loop

foreach ($fruits as $fruit) {
    echo $fruit . "\n";
}

Practical Example

Let's create an indexed array of numbers and calculate their sum:

$numbers = [1, 2, 3, 4, 5];
$sum = 0;

foreach ($numbers as $number) {
    $sum += $number;
}

echo "The sum of the numbers is: " . $sum; // Outputs: The sum of the numbers is: 15

Exercises

Exercise 1: Create and Access Indexed Array

  1. Create an indexed array with the names of five different animals.
  2. Print the name of the third animal in the array.

Solution

$animals = ["Cat", "Dog", "Elephant", "Giraffe", "Lion"];
echo $animals[2]; // Outputs: Elephant

Exercise 2: Modify and Add Elements

  1. Modify the second element in the array to "Wolf".
  2. Add a new animal "Tiger" to the array.
  3. Print all the elements in the array.

Solution

$animals[1] = "Wolf";
$animals[] = "Tiger";

foreach ($animals as $animal) {
    echo $animal . "\n";
}
// Outputs:
// Cat
// Wolf
// Elephant
// Giraffe
// Lion
// Tiger

Exercise 3: Calculate the Average

  1. Create an indexed array with five different numbers.
  2. Calculate and print the average of these numbers.

Solution

$numbers = [10, 20, 30, 40, 50];
$sum = 0;

foreach ($numbers as $number) {
    $sum += $number;
}

$average = $sum / count($numbers);
echo "The average is: " . $average; // Outputs: The average is: 30

Common Mistakes and Tips

  • Index Out of Range: Accessing an index that does not exist will result in an "undefined index" notice. Always ensure the index exists before accessing it.
  • Using count(): When looping through arrays, use the count() function to get the number of elements in the array.
  • Appending Elements: Use the empty brackets [] to append elements to the end of the array without specifying an index.

Conclusion

Indexed arrays are a fundamental concept in PHP, allowing you to store and manipulate lists of data efficiently. Understanding how to create, access, modify, and loop through indexed arrays is essential for any PHP programmer. In the next topic, we will explore associative arrays, which use named keys instead of numeric indices.

PHP Programming Course

Module 1: Introduction to PHP

Module 2: Control Structures

Module 3: Functions

Module 4: Arrays

Module 5: Working with Forms

Module 6: Working with Files

Module 7: Object-Oriented Programming (OOP)

Module 8: Working with Databases

Module 9: Advanced PHP Techniques

Module 10: PHP Frameworks and Best Practices

Module 11: Project: Building a Web Application

© Copyright 2024. All rights reserved