Arrays are fundamental data structures that allow you to store multiple values under a single variable name.

  1. Creating Arrays:

    $numbers = [1, 2, 3, 4, 5];
    $fruits = array("Apple", "Banana", "Orange");
    
    
  2. Accessing Elements:

    echo $numbers[0]; // Outputs: 1
    echo $fruits[2];  // Outputs: Orange
    
    
  3. Adding Elements:

    $numbers[] = 6;            // Adds 6 to the end of $numbers
    array_push($fruits, "Kiwi"); // Adds "Kiwi" to the end of $fruits
    
    
  4. Removing Elements:

    unset($numbers[3]);    // Removes element at index 3
    array_pop($fruits);    // Removes the last element from $fruits
    array_shift($numbers); // Removes the first element from $numbers
    
    
  5. Array Length:

    echo count($numbers); // Outputs: 5
    
    
  6. Iterating Through Arrays:

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

  8. Multi-dimensional Arrays:

    $matrix = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ];
    
    

Understanding these array operations and functions in PHP will empower you to write cleaner, more efficient, and maintainable code. Arrays are powerful tools for managing data structures, and mastering their usage is essential for proficient PHP development.