How to declare and use indexed arrays in PHP.
In PHP, an indexed array is a type of array that contains numerical keys, which can be declared and used in the following way:
Declare an index array.
$array = array("apple", "banana", "cherry", "date");
Using an index array:
echo $array[0]; // 输出:apple
echo $array[1]; // 输出:banana
echo $array[2]; // 输出:cherry
echo $array[3]; // 输出:date
You can use a loop to iterate through elements in an index array.
foreach ($array as $value) {
echo $value . "<br>";
}
You can also use a for loop to access elements in an indexed array.
for ($i = 0; $i < count($array); $i++) {
echo $array[$i] . "<br>";
}
You can use the array_push() function to add new elements to an indexed array.
array_push($array, "grape");
You can use the unset() function to remove elements from an indexed array.
unset($array[2]);
Note: The keys of indexed arrays start with 0 and increment by integers, allowing you to access the values of array elements by their index.