PHP Indexed Arrays

Learn via video courses
Topics Covered

Overview

Indexed arrays are one of the most commonly used data structures in PHP. They are used to store a collection of values that can be accessed using a numeric index. In an indexed array, each value is assigned a unique index, starting from zero for the first element and increasing by one for each subsequent element. Indexed arrays can be created using the array() function or by using square brackets to define the elements. They can be manipulated using various functions, such as array_push() and array_pop(), and can be looped over using a for loop or a for each loop. Overall, indexed arrays are a powerful tool for organizing and manipulating data in PHP.

Introduction

Indexed arrays in PHP are a fundamental data structure that allows developers to store and access multiple values using a single variable. As the name suggests, indexed arrays are arrays where each element is assigned an index or a key. The index is used to access a particular element in the array.

In PHP, indexed arrays are created using square brackets "[]" to define the array, followed by a comma-separated list of values enclosed in the brackets. The first element in the array is assigned an index of 0, the second element an index of 1, and so on. This means that to access the first element in the array, you would use the index of 0.

Indexed arrays in PHP are versatile and can be used in a variety of scenarios, such as storing a list of names, phone numbers, or products. They can also be used in more complex data structures such as multidimensional arrays, where each element is an indexed array itself.

One advantage of using indexed arrays in PHP is that they are easy to manipulate and iterate` through using loops. This makes them useful for tasks such as sorting, filtering, and searching through large datasets. Additionally, indexed arrays are lightweight and have a small memory footprint, making them ideal for use in web applications where efficiency and speed are important.

Create Indexed Arrays

Creating an indexed array in PHP is a simple process. There are two main ways to create an indexed array: using the array() function or using square brackets to define the elements. Let's see the syntax of indexed arrays in php: Using array() function:

Syntax

To create an indexed array using the array() function, you simply pass a comma-separated list of values to the function, enclosed in parentheses. Here is an example:

This creates an indexed array called $myArray that contains four elements: "apple", "banana", "orange", and "grape". Each element is assigned a unique index, starting from 0 for the first element and increasing by 1 for each subsequent element. Using square brackets:

Alternatively, you can create an indexed array using square brackets to define the elements. Here is an example:

This creates the same indexed array as the previous example but just by using a different method.

PHP Indexed Array Example

Here's a basic example of an indexed array in PHP:

Output

Explanation

In this example, we create a new indexed array called $fruits and initialize it with four elements using the array() function. The elements are "apple", "banana", "orange", and "grape", and they are assigned numeric indexes starting from 0. Run the above code in your editor for a better and clear explanation.

We then use the echo statement to output a string that includes the first and second elements of the $fruits array. We do this by using their indexes in square brackets, like this: $fruits[0] and $fruits[1].

We could have also used a for loop to loop through all the elements in the array and output each one individually, like this:

Output

Explanation

In this example, we use a for loop to iterate over each element in the $fruits array. The count() function returns the number of elements in the array, which we use as the upper bound of the loop. We then use the $i variable as the index to access each element of the array in turn and output it using the echo statement. Run the above code in your editor for a better and clear explanation.

We can also create an indexed array using the [] shorthand syntax, like this:

This creates the same array as the array() function example above, with the same elements and indexes.

More examples

Loop Through an Indexed Array

Here's an example of how to loop through an indexed array in PHP using a for loop:

Output

Explanation

In this example, we first create an indexed array called $fruits with four elements.

Next, we use a for loop to iterate over each element of the $fruits array. The loop begins with $i = 0, which is the index of the first element in the array. The loop continues as long as $i is less than the total number of elements in the array, which we get with the count() function. Inside the loop, we use the $i variable to access each element of the array, in turn, using the square bracket notation, like this: $fruits[$i]. Run the above code in your editor for a better and clear explanation.

Finally, we use the echo statement to output each element of the array on a separate line, with the <br> tag to create a line break. Run the above code in your editor for a better and clear explanation.

Note that we could also use a foreach loop to accomplish the same thing, like this:

Output

Explanation

In this example, we use a foreach loop to iterate over each element of the $fruits array. The loop assigns each element of the array in turn to the $fruit variable, which we then use with the echo statement to output each element of the array on a separate line, again with the <br> tag to create a line break. Run the above code in your editor for a better and clear explanation.

Count Length of PHP Indexed Array

Here's an example of how to count the length of a PHP-indexed array using the count() function:

Output

Explanation

In this example, we create a new indexed array called $fruits and initialize it with four elements. We then use the count() function to get the number of elements in the array, and store it in a new variable called $count.

Finally, we use the echo statement to output a string that includes the value of $count. Run the above code in your editor for a better and clear explanation.

Traversing PHP Indexed Array

Here's an example of how to traverse an indexed array in PHP:

Both of these loops will produce the same output:

Output

Explanation

In this example, we have an indexed array called $fruits with four elements. To traverse the array, we use two different types of loops: a for loop and a foreach loop.

The for loop iterates through the array using a numeric index, starting at 0 and ending at the length of the array minus 1. We use the count() function to get the length of the array. Inside the loop, we use the current index to access each element of the array using $fruits[$i], and then echo it out.

The foreach loop iterates through the array using a temporary variable called $fruit, which is set to each element of the array in turn. Inside the loop, we simply echo out the value of $fruit. Run the above code in your editor for a better and clear explanation.

Conclusion

  • Indexed arrays in PHP are a type of array where each element is assigned a numeric index starting from 0.
  • Indexed arrays can be created using the array() function or the [] shorthand syntax.
  • Elements can be added to an indexed array using the $array[] = value syntax.
  • Indexed arrays can be traversed using for and foreach loops.
  • The count() function can be used to get the length of an indexed array.
  • Indexed arrays are useful for storing collections of data that can be easily accessed and manipulated using their indexes.