What is an indexed array in php?


PHP Indexed Arrays

There are two ways to create indexed arrays:

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";
$cars[1] = "BMW";
$cars[2] = "Toyota";

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");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

Try it Yourself »


Loop Through an Indexed Array

To loop through and print all the values of an indexed array, you could use a for loop, like this:

Example

$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {
  echo $cars[$x];
  echo "
";
}
?>

Try it Yourself »



Complete PHP Array Reference

For 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 Exercises



An 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 0 for the first array element.


Creating an Indexed Array

There 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 Array

Traversing 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 for loop or foreach. To know the syntax and basic usage of for and foreach loop, you can refer to the PHP for and foreach loop tutorial.


Using for loop

While using the for loop to traverse an indexed array we must know the size/length of the array, which can be found using the count() function.

Following is the syntax for traversing an array using the for loop.


Using the foreach loop

using foreach to traverse an indexed array is a better approach if you have to traverse the whole array, as we do not have to bother about calculating the size of the array to loop around the array.

Below we have used the foreach to traverse the $lamborghinis array.


Advantages of Indexed Array

Here are a few advantages of using indexed array in our program/script:

  1. Numeric index values make it easier to use and having numeric indexes are common to almost all the programming languages, hence it also makes the code more readable for others who go through your code.


In this tutorial you'll learn how to store multiple values in a single variable in PHP.

What is PHP Arrays

Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. Let's suppose you want to store colors in your PHP script. Storing the colors one by one in a variable could look something like this:

But what, if you want to store the states or city names of a country in variables and this time this not just three may be hundred. It is quite hard, boring, and bad idea to store each city name in a separate variable. And here array comes into play.


Types of Arrays in PHP

There are three types of arrays that you can create. These are:

  • Indexed array — An array with a numeric key.
  • Associative array — An array where each key has its own specific value.
  • Multidimensional array — An array containing one or more arrays within itself.

Indexed Arrays

An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is:

Note: In an indexed or numeric array, the indexes are automatically assigned and start with 0, and the values can be any data type.

This is equivalent to the following example, in which indexes are assigned manually:


Associative Arrays

In an associative array, the keys assigned to values can be arbitrary and user defined strings. In the following example the array uses keys instead of index numbers:

22, "Clark"=>32, "John"=>28);
?>

The following example is equivalent to the previous example, but shows a different way of creating associative arrays:


Multidimensional Arrays

The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. An example of a multidimensional array will look something like this:

 "Peter Parker",
        "email" => "",
    ),
    array(
        "name" => "Clark Kent",
        "email" => "",
    ),
    array(
        "name" => "Harry Potter",
        "email" => "",
    )
);
// Access nested value
echo "Peter Parker's Email-id is: " . $contacts[0]["email"];
?>


Viewing Array Structure and Values

You can see the structure and values of any array by using one of two statements — var_dump() or print_r(). The print_r() statement, however, gives somewhat less information. Consider the following example:

The print_r() statement gives the following output:

Array ( [0] => London [1] => Paris [2] => New York )

This output shows the key and the value for each element in the array. To get more information, use the following statement:

This var_dump() statement gives the following output:

array(3) { [0]=> string(6) "London" [1]=> string(5) "Paris" [2]=> string(8) "New York" }

This output shows the data type of each element, such as a string of 6 characters, in addition to the key and value. In the next chapter you will learn how to sort array elements.

You will learn how to loop through the values of an array in the later chapter.

What is an indexing array?

Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.

What is the difference between indexed and associative array in PHP?

Indexed array: Indexed array is an array with a numeric key. It is basically an array wherein each of the keys is associated with its own specific value. ... PHP..

What is a 1 indexed array?

Zero-based array indexing is a way of numbering the items in an array such that the first item of it has an index of 0, whereas a one-based array indexed array has its first item indexed as 1. Zero-based indexing is a very common way to number items in a sequence in today's modern mathematical notation.

What are the three types of arrays in PHP?

Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.