In the following article, we will discuss on Indexed Array in PHP. An array is a data structure or, more like a holding place as discussed above, is such which stores one or more same types of data under a single name. Another way to understand this is that we have the key to every value in our structure array, so when our single variable holds a list of items or values, we can identify each of them using these keys. Such optimization of this data structure can be used as an array or a dictionary or a collection of values, stack-queue, etc. And since the values inside our array can also be an array itself, there is a possibility we might create a tree or a multidimensional array.
Now, based on size and the type of keys used in our array, which can be either a string or an integer, there are mainly three types in which we create our arrays. The value can be created of any type. Thus, the types can be created as, namely, Numeric or Indexed Array, Associative Array, and Multidimensional Array.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
As briefly discussed above, Indexed Array is a type of array with numeric indices to access its values. However, they can store numbers, characters, strings, etc. By default, the array indices are represented by numbers if not specified, and it starts with index 0 and ends with index -1.
There are mainly two ways we can create an indexed array.
Let us see both the methods of creating an array, one by one.
Manual Index Assignment: In the following given an example, we have manually assigned indexes, one by one, to our values here.
<?php $employee[0] = "Ram"; $employee[1] = "Male"; $employee[2] = "28"; echo "My name is ".$employee[0].", I am ".$employee[2] . " years old and my gender is ".$employee[1]."."; ?>
The above example code will produce output as:
You can also see this same code in the below-given screenshot of the program and its output in a live environment.
Function array(): The below written code is creating an indexed array named $autos using array () function. The function is assigning three elements to our array name.
We then formed a simple text line containing the array values and printed them using echo statements.
Code:
<?php $employee = array("Ram", "Male", "28"); echo "My name is ".$employee[0].", I am ".$employee[2] . " years old and my gender is ".$employee[1]."."; ?>
Output:
Note: We accessed the $employee[2] index first, and then we called $employee[1] according to our need.But what if I have dozens of values inside my array and I need to print them?
It will be bothersome to type all the values from the array using separators with echo statements to print them all. For this, an easy way out is if we can traverse through our complete array and be able to print the values. Traversing through the indexed array is simple and easy in the indexed array; here, we make use of loops.
Traversing an array means reading the values of the array one by one and printing if needed. Indexed Arrays can be easily traversed; we simply use the “looping through the values” method. We will use for loop or for each loop for traversing our indexed array and then print all the required values.
Code:
<?php $employee = array("Ram", "Male", "28"); $length = count($employee); for($x = 0; $x < $length; $x++) { echo $employee[$x]; echo "<br/>"; } ?>
Output:
The above program prints the contents of our array.
Note: The values Ram, Male and 28 are printed in new lines because of the break statement (Code:
<?php $employee = array("Ram", "Male", "28"); foreach($employee as $e) { echo "$e <br/>"; } ?>
Output:
You can see the above simple code and its output in the live environment in the following screenshot.
Another commonly used method in arrays is to fetch the length of the array. The count() function is used for this purpose. Following is a simple PHP code creating an array and then returning its length. We used the count() function, which is returning the length, i.e. the number of elements our array contains, as shown in the output.
Code:
<?php $employee = array("Ram", "Male", "28"); echo count($employee); ?>
Output:
The output is 3 (see in the above screenshot), which is equal to the total number of elements or values in our array $employee.
In simple words, arrays tend to show special characteristics with a capacity to store several values in one variable. They are quite stretchable; that is, if one needs to add more values afterward, it can be done with ease. An indexed array is a smarter way to bind all related information together, for example, an employee’s details. It also helps in writing clean code.
The above is the detailed content of Indexed Array in PHP. For more information, please follow other related articles on the PHP Chinese website!