What is an indexed array in php?
PHP Indexed ArraysThere are two ways to create indexed arrays: Show
The index can be assigned automatically (index always starts at 0), like this: $cars = array("Volvo", "BMW", "Toyota"); or the index can be assigned manually: $cars[0] = "Volvo"; The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values: Example $cars = array("Volvo", "BMW", "Toyota"); Try it Yourself » Loop Through an Indexed ArrayTo loop through and print all the values of an indexed array, you could use a Example
$cars = array("Volvo", "BMW", "Toyota"); for($x = 0; $x < $arrlength; $x++) { Try it Yourself » Complete PHP Array ReferenceFor a complete reference of all array functions, go to our complete PHP Array Reference. The reference contains a brief description, and examples of use, for each function! PHP ExercisesAn indexed array is a simple array in which data elements are stored against numeric indexes. All the array elements are represented by an index which is a numeric value starting from Creating an Indexed ArrayThere are two different ways of creating an indexed array in PHP, they are, Syntax for the 1st way to create indexed array:
and the syntax for the 2nd way to create indexed array is:
No matter how we initialize the array, we can access the array as follows,
Accessing the 2nd array... Swift Brezza Ertiga Accessing the 1st array... Huracan Urus Aventador Hence, to access an indexed array, we have to use the array name along with the index of the element in square brackets. Traversing PHP Indexed ArrayTraversing an array means to iterate it starting from the first index till the last element of the array. We can traverse an indexed array either using a Using for loopWhile using the Following is the syntax for traversing an array using the
Using the |