The 3 types of arrays in PHP

Summary:

There are 3 types of PHP arrays. Indexed arrays provide a sequential list of values, associative arrays provide a key-value pair like a hash map, and multi-dimensional arrays are arrays that contain other arrays.

Introduction – Types of Arrays in PHP

Arrays are the fundamental data types for storing multiple values. All programming languages feature an array as the core data type, with slight variations. For instance, Java keeps the same data type values in an array. An array in PHP is more flexible in terms of the data types as it can store mixed types in an array. There are three different types of arrays in PHP.

  • Indexed array PHP
  • Associative array PHP
  • Multidimensional array PHP

This article overviews these types of arrays in PHP with examples. Besides the types, the article includes additional sections related to the PHP array. So, there is a lot of ground to cover, and that’s why it’s time to move on to the main subject.

Types of Arrays in PHP

As seen earlier, the array in PHP is one of the three types. Let’s start with the indexed array PHP.

#1 – Indexed Array PHP

These are the most common array type that occurs in other programming languages too. PHP explicitly or implicitly keeps a numeric index for the array values. These PHP arrays can be initiated using the array() keyword, the square bracket [] syntax, or through a manual assignment.

types of arrays in PHP

Here’s the example of all three in one place.

<?php
 
//Using the array() keyword
$array_first  = array("One", 2, 3.0, 'Four', 5);
 
//Using the square bracket syntax
$array_second = ["Six", "Seven", 8.0, 9];
 
//Using the direct assignment
$array_third[0] = 'Ten';
$array_third[1] = 11;
$array_third[2] = "Twelve";
$array_third[3] = 13.0;
 
 
?>

Indexed array PHP has a numeric index. It is clear when it prints the array out to the console.

/*
OUTPUT
Array
(
    [0] => One
    [1] => 2
    [2] => 3
    [3] => Four
    [4] => 5
)
*/

That is the output of the $array_first from the example. The article explains how to print a PHP array in a later section. That’s about the indexed array PHP. Next, let’s jump to the associative array PHP.

#2 – Associative Array PHP

Associative array PHP is known for keeping “KEY/VALUE” pairs. Programming languages, for instance, Python implements the dictionary for “KEY/VALUE” pairs.  However, PHP doesn’t distinguish there and assumes every array as associative.

types of arrays in PHP

As a matter of fact, the indexed array is also associative. PHP implicitly defines numeric keys for the values. Associative arrays can be initialized using the same three ways but explicitly defining the keys rather than listing the values. Here’s an example.

//Using the array() keyword
$emp_age_first = array("Mark"=>32, "Lisa"=>28, "Alan"=>5);
 
//Using the square bracket syntax
$emp_age_second = ["Sophie"=>27, "Richard"=>20, "David"=>30];
 
//Using the direct assignment
$emp_age_third["Frankil"] = 32;
$emp_age_third["Roman"] = 19;
$emp_age_third["Kylie"] = 25;

Here’s the output for $emp_age_third.

/*
OUTPUT
Array
(
    [Frankil] => 32
    [Roman] => 19
    [Kylie] => 25
)
*/

Let’s move to the third PHP array type, the multidimensional array.

#3 – Multidimensional Array PHP

A multidimensional array PHP has nested arrays. Nested arrays mean arrays inside arrays. It is synonymous with the matrix in mathematics. So, multidimensional arrays have multiple indexes, just as a matrix has rows and columns. Here’s an example.

//Using the array() keyword - 3x3
$integers_arr = array(
        array(1, 2, 3),
        array(4, 5, 6),
        array(7, 8, 9),
);
 
//Using the square bracket syntax - 2x3
$float_arr = [
              [1.0, 2.0],
              [3.0, 4.0]
             ];
 
//Using the direct assignment-2x2
$string_arr[0] = array('One', 'Two');
$string_arr[1] = array('Three', 'Four');

There could be even more nesting but let’s just keep it simple here. So, the arrays need more than one index for directly accessing a value. Let’s try to access ‘1.0’ from the $float_arr.

//Output: 1.0
print_r($float_arr[0][0]);

So, the first zero index gets the first array; call it $first. The second zero index gets the first value of the $first array. Moreover, string keys can also be used in the multidimensional array PHP. 

So, that’s all the three types of arrays in PHP. Wonder how PHP prints an array out to the console. Let’s check that out.

How to Print an Array in PHP?

There are more than one ways to print an array to the console. Let’s see the one that’s most convenient and readily available. PHP has the print_r function to print a human-readable output of a PHP array. Here’s an example.

//An associative array with employee-age as key-value pairs.
$employee_age = array("Anna"=>21,"Mike"=>24,"Benjamin"=>25,"Mark"=>29,"Sarah"=>30);
 
//Prints the $employee_age array
print_r($employee_age);

Here’s the output on the console.

PHP array

Voila! That’s not all because PHP has lots of excellent functions for arrays. Next, let’s see an overview of how to loop through a PHP array.

How to loop through a PHP array

PHP features several loop constructs to iterate through an array. These loops are.

  • foreach loop
  • for loop
  • while loop
  • do-while loop

The foreach loop is ideal for the PHP associative array because it keeps reference for both keys and variables while looping through an array.

<?php
 
//An array containing employee-age as key-value.
$employee_age = [
    "Peter" => 22,
    "Anna" => 25,
    "Steve"   => 30,
    "Bob"  => 32,
    "Mark" => 35]; 
 
//foreach loop to iterate over the $employee_age array.
foreach($employee_age as $employee => $age)
{
    echo $employee." is ".$age." years old."."\n";
}
 
/*
OUTPUT
Peter is 22 years old.
Anna is 25 years old. 
Steve is 30 years old.
Bob is 32 years old.  
Mark is 35 years old. 
 */
?>

Awesome! PHP array has many functions. At FuelingPHP, there are tons of helpful articles related to PHP. Make sure to check them out.

Conclusion – Types of Arrays in PHP

This article overviews the types of arrays in PHP with examples. The three array types are indexed array, associative array, and multidimensional array. Furthermore, the article includes some information about printing PHP arrays to the console and iterating through an array using loops.

Hopefully, you have learned something new today. If you did, make sure to check more informative articles about PHP at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

  Was this information helpful?

Can Your Career Use a Boost?

Check out our free PHP developer career guide. We created this guide as a resource to help fellow programmers level up their careers. This is an excellent resource whether you are new to programming or have been a professional for more than 15 years.

Click here to read the guide


Article Categories

Article Tags

© 2022 Confident.Systems