Home >Backend Development >PHP Tutorial >Indexed Array in PHP

Indexed Array in PHP

WBOY
WBOYOriginal
2024-08-29 12:44:371201browse

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.

Types of 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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Indexed Array in PHP

  1. Indexed or Numeric Array: A type where our keys are numeric in nature, i.e. numeric indices. The values here are accessed linearly and stored in a linear manner.
  2. Associative Array: This type of array has a string as its keys to access values and a strong association between its keys and values. It is similar to an indexed array.
  3. Multidimensional Array: A multidimensional array could contain an array that might contain another array inside and so on. In the multidimensional array, values are accessed using different indices.

Indexed Array

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.

  1. The first is to simply assign an index to every value ‘manually’ and create your array.
  2. Second, we can use the array() function without any indices, and the index will get assigned by default and will start at 0 for the first element or value. I personally go for this method.

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:

Indexed Array in PHP

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:

Indexed Array in PHP

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 Indexed Array in PHP

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:

Indexed Array in PHP

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 (
) we used in our code.

Code:

<?php
$employee = array("Ram", "Male", "28");
foreach($employee as $e)
{
echo "$e <br/>";
}
?>

Output:

Indexed Array in PHP

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:

Indexed Array in PHP

The output is 3 (see in the above screenshot), which is equal to the total number of elements or values in our array $employee.

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn