Indexed Array in PHP
An indexed array in PHP is a type of array which stores values or data in an ordered manner. It is one of the most common and powerful ways to store collections of data.
In the indexed array, each element is referenced by the numeric index, also known as numeric keys. Therefore, this array is also known as a numeric array in PHP.
An indexed array uses the numeric keys to access elements. By default, the indexed array starts with 0 index value for the first element and increments by 1 for each element. For instance, the first element has an index of 0, the second has an index of 1, and so on.
PHP automatically sets the index of an indexed array, but you can also set it manually. Note that an indexed array always uses numeric keys (e.g., 0, 1, 2, 3, etc.) which is assigned automatically or manually.
If you use string keys in an array, PHP automatically considers it as an associative array. Therefore, you should use indexed array when you want to work with purely numeric keys rather than string keys.
How to Create an Indexed Array in PHP?
There are several ways to create indexed arrays in PHP. Here are some common methods:
1. Using the array() Function
The general syntax to declare an indexed array using a built-in array() function is as follows:
$array_name = array(comma-separated list of array elements);
In the above syntax of indexed array, $array_name represents the name of an array variable. The array() is the array function used to define array elements inside the parentheses.
Example 1:
$num = array(100, 200, 300, 400);
In this example, 100, 200, 300, and 400 are the array elements referred by a common array variable name $num.
2. Using Short Hand Operator
PHP 5.4 version has introduced a short hand operator ([]) to create an indexed array. The general short hand syntax for declaring an indexed array is as follows:
$array_name = [comma-separated list of array elements];
This short hand syntax of indexed array is exactly the same as the previous syntax, with only one difference that square brackets ([]) are used instead of array() function to define array elements inside it.
Example 2:
$names = ["John", "Saanvi", "Mark", "Bob"];
The working of this example is the same as the previous example with the only difference is that square brackets [ ] are used to define array elements and is generally treated more modern and cleaner.
3. Manually Adding Array Elements
The general syntax to generate the index of indexed array and add elements in the array is as follows:
$array_name[index_number] = "element";
In the above syntax, $array_name[] represents the array variable used to define the index number inside the pair of square brackets. Here, the index number is the numeric value, which specifies index of an element set manually.
Example 3:
$animals[0] = "Dog";
$animals[2] = "Cat";
$animals[1] = "Elephant";
In the given example, $animals[0] is an array variable which is holding a string type value “Dog” and its index position is set manually to 0. Similarly, “Cat” and “Elephant” are at the index position 2 and 1, respectively.
This approach is useful when you want to maintain a custom index mapping for better readability. You do not need non-sequential or specific indices for the array elements.
There is also an alternate method to manually set the index value of array elements in PHP. The general syntax is given below:
$array_name = array (
index0 => element1,
index1 => element2,
index2 => element3,
. . . . . .
. . . . . .
);
In the above syntax, index0, index1, index2, . . . . specifies the numeric index position of the array elements in the array. element1, element2, element3, etc. are the values assigned to the respective indices. The operator => is used to assign an index number to a specific element.
Example 4:
$fruits = array(
10 => "Apple",
20 => "Banana",
30 => "Cherry"
);
In this example, $fruits is the name of an array variable. Here, “Apple” is a string type value and its index position is set manually to 10 by using => operator. Similarly, “Banana” and “Cherry” are set to index position 20 and 30, respectively. These are the common ways to create an indexed array in PHP. You can use any of given syntax.
Accessing Elements in Indexed Array in PHP
After allocating memory to an array, you can access any of its elements in an indexed array by using the index value. The general syntax to access an element of an indexed array in PHP is as follows:
$array_name[index_value];
In the above syntax, $array_name specifies the array and the index number specifies the index position where the array element is stored. The index number can be from 0 to n-1. Here, n specifies the total number of elements in the array.
Example 5:
<?php
// Create an array of four elements.
$fruits = ["Apple", "Banana", "Cherry", "Orange"];
// Accessing array elements one by one.
echo $fruits[0] . "\n";
echo $fruits[1] . "\n";
echo $fruits[2] . "\n";
echo $fruits[3];
?>
Output: Apple Banana Cherry Orange
Example 6:
<?php
// Create an array of four elements.
$fruits = [0 => "Apple", "1" => "Banana", 2 => "Cherry", 3 => "Orange"];
// Displaying array elements.
print_r($fruits);
?>
Output: Array ( [0] => Apple [1] => Banana [2] => Cherry [3] => Orange )
In this example, we have used a string key “1” which are automatically converted to integers and treated as numeric keys.
Common Operations on Indexed Arrays
Let’s perform some common operations, such as modifying array elements, adding elements to an array, removing elements from an array, etc.
1. Modifying Array Elements:
You can modify an existing element of an indexed array by assigning new value to it.
Example 7:
<?php
$numbers = [1, 2, 3, 4, 5];
$numbers[2] = 10;
print_r($numbers);
?>
Output: Array ( [0] => 1 [1] => 2 [2] => 10 [3] => 4 [4] => 5 )
2. Adding Elements to Array:
To add a new element in an indexed array, you can use the square bracket operator ([]) without specifying an index.
Example 8:
<?php
$foods = ["Pizza", "Burger", "Momos"];
$foods[] = "Pasta"; // Adding a new element in the array.
print_r($foods);
?>
Output: Array ( [0] => Pizza [1] => Burger [2] => Momos [3] => Pasta )
As you see in the output, the “Pasta” element is added to the end of the $foods array. PHP automatically add a new element to the end i.e. the next available index, which in this case is 3.
3. Counting Elements in an Array:
You can use count() function to find out how many elements there are in an array.
Example 9:
<?php
$foods = ["Pizza", "Burger", "Momos"];
$foods[] = "Pasta";
echo count($foods);
?>
Output: 4
4. Removing an Array Element:
You can remove elements from an indexed array in PHP using the unset() function.
Example 10:
<?php
$numbers = [10, 20, 30, 40, 50];
// Removing an element from the array.
unset($numbers[2]);
print_r($numbers);
?>
Output: Array ( [0] => 10 [1] => 20 [3] => 40 [4] => 50 )
Note that the indices are not reindexed automatically.
5. Reindexing an Array:
You can reindex an array using the array_values() function provided by PHP.
Example 11:
<?php
$numbers = [10, 20, 30, 40, 50];
// Removing an element from the array.
unset($numbers[2]);
// Re-indexing an array.
$numbers = array_values($numbers);
print_r($numbers);
?>
Output: Array ( [0] => 10 [1] => 20 [2] => 40 [3] => 50 )
These are some common operations that we have performed in an indexed array in PHP. You should practice them.