In PHP, an indexed array is an array where each element is assigned a numerical index starting from zero. Indexed arrays are the most common type of arrays used in PHP.
Here’s an example of how to create and access an indexed array:
// Create an indexed array $fruits = array("apple", "banana", "orange"); // Access elements in the array echo $fruits[0]; // Output: apple echo $fruits[1]; // Output: banana echo $fruits[2]; // Output: orange
In this example, we create an array of fruits with three elements and assign it to the variable $fruits. We then use the index number to access each element in the array and output its value using echo.
You can also add elements to an indexed array dynamically using the [] syntax:
// Create an empty indexed array $numbers = array(); // Add elements to the array $numbers[] = 1; $numbers[] = 2; $numbers[] = 3; // Access elements in the array echo $numbers[0]; // Output: 1 echo $numbers[1]; // Output: 2 echo $numbers[2]; // Output: 3
In this example, we create an empty indexed array called $numbers and then add three elements to it using the [] syntax. We then use the index number to access each element in the array and output its value using echo