Get DevWP - WordPress Development Theme

PHP Arrays

In this tutorial I will demonstrate all three types of arrays with code examples explained.

What Are Arrays in PHP

  • An array is a data type that stores multiple values as a key-value pair, in a single variable.
  • Arrays are used when you want to store more than one value without the need to create multiple individual variables.
  • Arrays are a great way to organize related data/information and are a vital part of programming in PHP.

Below is some pseudocode to show you the structure of an array.

    key  => value,
    key2 => value2,
    key3 => value3,

Arrays sperate key-value pairs with a comma followed by the next key-value pair. The last key-value pair doesn’t need to be followed by a trailing comma but you might want to add it just in case you dynamically add more key-value pairs.

Types of Arrays in PHP

There are three types of arrays in PHP:

  • Indexed Arrays – an array with a numeric key
  • Associative Arrays – an array with strings as keys
  • Multidimensional Arrays – an array with nested arrays

Indexed Array in PHP

Below are two basic examples of an array stored in a variable called $names and $names2.

Using the older style of creating an array.

$names = array( "Joel", "Joyce", "John", "Jane", "Jill" );

Using the newer style of creating an array.

$names2 = [ "Joel", "Joyce", "John", "Jane", "Jill" ];

Above is an example of an Indexed Array where you can see the values. But what about the keys? As I mentioned, arrays store multiple values in a key-value pair.

In an Indexed Array, integers are used as the keys. Below is another example where I simply show you the keys.

$names3[0] = "Joel";
$names3[1] = "Joyce";
$names3[2] = "John";
$names3[3] = "Jane";
$names3[4] = "Jill";

echo "There are 5 names in the names3 array() which are $names3[0], $names3[1], $names3[2], $names3[3] and $names3[4]";

You’ll notice that in an indexed array, the first key is 0 and not 1 as you might expect. This can be confusing at first when learning how to work with arrays, but in time, you get used to it.

Associative Array in PHP

In an Associative Array, strings are used for keys instead of integers.

$names4 = [ "Joel"  => "Web Developer",
            "Joyce" => "Social Media",
            "John"  => "Consultant",
            "Jane"  => "Customer Support",
            "Jill"  => "Outreach"

print_r( $names4 );

Multidimensional Array in PHP

Now let’s take a look at Multidimensional Arrays which can get to be very complex depending on how many nested arrays you’re working with.

Basically, a Multidimensional Array is an array of arrays.

// create the multidimensional array
$contacts = array(
		"name"  => "Joel",
		"email" => "",
		"name"  => "Joyce",
		"email" => "",
		"name"  => "John",
		"email" => "",
// access nested value
echo "Joel's Email is: " . $contacts[0]["email"];

The echo command above is getting the first nested array which is in position 0 and then accessing the email from the associative array with the string key “email”.

Let’s look at another example, but this time, accessing the 3rd nested array.

echo "John's Email is: " . $contacts[2]["email"];

Debugging or Outputting Array Structure and Values

There are a few useful functions in PHP to view the structure and Values of a PHP Array.

  • var_dump()
  • print_r()

Both functions are used to output the structure and values of a PHP Array. Note: these should only be used in development and not in production.

View Our Themes