In PHP, arrays are a fundamental data structure that can store multiple values in a single variable. There are two main types of arrays in PHP: indexed arrays and associative arrays. This tutorial will focus on indexed arrays, which use numeric keys.
Indexed arrays are the simplest type of array where each element is assigned a numeric key starting from 0. They are similar to lists or vectors found in other programming languages. Understanding indexed arrays is crucial for handling collections of data efficiently in PHP.
An indexed array in PHP is an ordered collection of values, where each value has a corresponding numeric index. The first element has an index of 0, the second element has an index of 1, and so on. This numeric indexing allows you to access elements directly using their position in the array.
Indexed arrays are particularly useful when you need to store a list of items that do not require named keys. They provide fast access to elements based on their position and are ideal for scenarios where order matters.
Let's explore some practical examples to illustrate how indexed arrays work in PHP.
You can create an indexed array using the array() function or the shorthand syntax [].
array() Function$fruits = array("Apple", "Banana", "Cherry");
$fruits = ["Apple", "Banana", "Cherry"];
Both methods create an indexed array with three elements: "Apple" at index 0, "Banana" at index 1, and "Cherry" at index 2.
You can access elements in an indexed array using their numeric keys.
echo $fruits[0]; // Outputs: Apple
echo $fruits[1]; // Outputs: Banana
To add elements to an indexed array, you can use the [] operator or specify a new index directly.
[] Operator$fruits[] = "Date"; // Adds "Date" at the end of the array
$fruits[3] = "Elderberry"; // Adds "Elderberry" at index 3
You can modify elements in an indexed array by assigning a new value to their existing index.
$fruits[1] = "Blueberry"; // Changes "Banana" to "Blueberry"
Indexed arrays are often used in loops to iterate over their elements. PHP provides several ways to loop through arrays, such as foreach, for, and while.
foreachforeach ($fruits as $fruit) {
echo $fruit . "\n";
}
<OutputBlock>
{`Apple
Banana
Cherry
Date
Elderberry`}
</OutputBlock>
#### Using `for`
```php
for ($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i] . "\n";
}
<OutputBlock>
{`Apple
Banana
Cherry
Date
Elderberry`}
</OutputBlock>
### Removing Elements
To remove elements from an indexed array, you can use the `unset()` function.
```php
unset($fruits[1]); // Removes "Blueberry" (original "Banana")
After this operation, the array will look like this:
print_r($fruits);
Array ( [0] => Apple [2] => Cherry [3] => Date [4] => Elderberry )
Notice that the keys are not re-indexed. If you want to re-index the array, you can use array_values().
$fruits = array_values($fruits);
print_r($fruits);
Array ( [0] => Apple [1] => Cherry [2] => Date [3] => Elderberry )
Now that you have a solid understanding of indexed arrays, the next step is to explore associative arrays. Associative arrays allow you to assign custom keys to each element, providing more flexibility and readability in your code.
Stay tuned for the next tutorial where we will dive into associative arrays and how they differ from indexed arrays.