Introduction

Arrays are consecutive blocks of memory locations to store data in them. PHP provides one-dimensional as well as multi-dimensional arrays, that can be in key-value form. In this article, you will learn about PHP arrays in depth. To build a strong base in PHP, it’s very important to have a clear concept of PHP arrays. So, let’s dig into it.

What happens when data is stored in Arrays?

Try to imagine the computer memory as a collection of blocks. Creating an array of size n occupies n number of consecutive memory blocks for this array. The starting block is at index 0 and the last block of the array is at index n-1. Where n is the size of the array.

With this idea of the array inside the memory, there are three major types of arrays in PHP.

  1. Indexed Arrays
  2. Associative Arrays
  3. Multi-dimensional Arrays

How to create an array in PHP

Arrays are built-in in PHP, you don’t need to install any library. Simply use the array() keyword to create an array in PHP. See the example below.


 $my_first_array = array( 1, 2, 3, 4, 5);
 /* Another method to create array. */
    $array[0] = "one";
    $array[1] = "two";
    $array[2] = "three";

Indexed arrays in PHP

Indexed or numeric index arrays store that data and point it using an index that starts from 0 and points at the first element of the array.

Example of the indexed array in PHP

<?php
$bikes = array("Suzuki", "Yamaha", "Honda");
echo "I like " . $bikes[0] . ", " . $bikes[1] . " and " . $bikes[2] . ".";
?>

Explanation of example

  • The array is formed by using the array() keyword. The values are separated by commas.
  • To access the array values, pass the index of the value in square brackets.
  • Loop through PHP indexed array

Consider the above example and loop through the array values one by one.

foreach( $bikes as $bike ) {
   echo "I like $bike <br />";
 }

Associative arrays in PHP

These are some of the most important and widely used concepts of PHP. Associative arrays are just like numeric or indexed arrays but they don’t have a numeric index for each value. Each value of the associative arrays is assigned a string key. In this way, we have a strong association between the keys and values.

Example of Associative arrays in PHP

<?php
  $employee = array("Jawad" => "Tech", "Adam" => "Tech", "Tuba" => "Tech");
  echo "Jawad works in ". $employee['Jawad'] . "department" <br />";
  echo "Jawad works in ". $employee['Adam'] . "department" <br />";
  echo "Jawad works in ". $employee['Tuba'] . "department" <br />";
?>

Loop through Associative array in PHP

Consider the above array to loop through it. You will see, the number of lines of code reduces by using Foreach loop to access associative arrays.

foeach($employee as $key=>$value)
{
  echo $key . "works in" . $value <br />";
}

In the above example, the key holds the index of the associative array and the value holds the value against this key.

Multi-Dimensional Arrays in PHP

Arrays containing further arrays are called multi-dimensional arrays. At this level, we will teach you the two-dimensional arrays in PHP. However, the number of dimensions can be more than two, but they are more complex. Hence, not required at this level.

$weather = array (
  array("Jan","cool"),
  array("Feb","moderate"),
  array("Jun","hot"),
  array("Oct","moderate")
);

In the above example, the main array is $weather. It contains four arrays inside it. Let’s see, how to access the values of two-dimensional arrays in PHP.

echo "weather in " $weather[0][0] is "$weather[0][1]";

echo "weather in " $weather[1][0] is "$weather[1][1]";

echo "weather in " $weather[2][0] is "$weather[2][1]";

echo "weather in " $weather[3][0] is "$weather[3][1]";

Loop through multi-dimensional arrays in PHP

Consider the above example to loop it using for loop in PHP.

for($i = 0; $i <= 3 ; $i++)
{
  echo "weather in " $weather[$i][0] is "$weather[$i][1]";
}

How to get the length of the array in PHP

PHP count() function returns the length of the array or the string passed in it. The array can be either index, associative or multi-dimensional.

<?php
$bikes= array("Honda", "Suzuki", "Yamaha");
echo count($bikes);
?>

PHP 8 has been released and arrays are the same as in the previous versions of PHP.

In this article, you have gone through the arrays, their types, accessing the arrays with and without the loops. There are many other concepts that revolve around arrays in PHP. Like sorting of arrays elements, reversing the elements of an array, etc. We will cover that in the next part of the array tutorial.

Note: If you have an array of size 10 with elements stored in it. Hence, try to access the array by giving an index greater than 10 or less than 0. It will give an index_out_of_bound error.

Range function in PHP

The range is an interesting function when discussed with the arrays. It has some pretty good use cases. Range function creates an array within the range specified. Remember that, the result includes the starting and ending values. The step parameter defines the increment or decrement for the next element in the range.

foreach (range(0, 5) as $num) {
    echo $num; // array(0, 1, 2, 3, 4, 5)

}

// Using step parameter
foreach (range(0, 50, 10) as $num) {
    echo $num; // array(0, 10, 20, 30, 40, 50)

}
foreach (range('a', 'f') as $char) {
    echo $char; // array('a', 'b', 'c', 'd', 'e', 'f');
}

For official PHP 8 arrays reference, click here.