October 29, 2026

PHP – Arrays

An array is a data structure used to hold one or more values of similar data types. An array is define using a single name. So for instance, if you have 100 different student scores, you would not have to create 100 different variables, you just use a single variable name.

In this tutorial, we would examine the following three different types of arrays:

  1. Numeric Arrays
  2. Associative Arrays
  3. Multidimensional Arrays

 

1. Numeric Arrays

These are arrays that has numeric index (not that they store numbers!) Numeric arrays could hold any kind of values like strings, numbers and objects. Let’s take an example to clarify it. In the code below, two numeric arrays are created and printed out to the screen.

<?php
    //declaring the array is not mandatory
    $scores[0] = 56;
    $scores[1] = 98;
    $scores[2] = 79.6;

    foreach($scores as $score){
        echo "$score <br>";
    }

    //a numeric array of strings
    $names = array("MacMills", "Trust", "Janai");
    foreach($names as $name){
        echo "$name <br>";
    }
?>     

As an exercise, execute this code see what output you have.

 

2. Associative Arrays

The associative arrays are similar to the numeric arrays. But unlike the numeric arrays, the associative arrays have their indexes as strings. This means that you can relate the indexes with their values.

For example,  assuming we want to store the test scores of students, we could use an associative array where we have the names of the students as the keys(or indexes) and the values would be the scores of the students.

The code below illustrates this:

<?php
    //Creating an associative array - Method 1
    $scores = array("Kindson"=>99.9, "Saff"=>89.4, "Ottie"=>87.9);

    echo "Kindson's score is ". $scores['Kindson'] . "<br />";
    echo "Saff's score is ".  $scores['Saff']. "<br />";
    echo "Ottie's score is ".  $scores['Ottie']. "<br />";
    
    //Method 2
    $scores["Kindson"] = 99.9;
    $scores["Saff"] = 89.9;
    $scores["Ottie"] = 87.9;
?> 

The result of this code is:

Kindson's score is 99.9
Saff's score is 89.4
Ottie's score is 87.9

 

3. Multidimensional Arrays

A multidimensional array allows you to have arrays as elements of an array. We call this “array of arrays”. To access values in multidimensional arrays, we use multiple indexes.

The example below expands on the previous example. In this case, Each element of $scores will be an associative array of 3 scores of that student on three different subjects. Take some time to understand it.

<?php
    //Creating an multidimensinal array
    $scores = array(
    "Kindson"=>array("Physics" => 99.4, "Math" => 84.3, "Chemistry" => 98.6),
    "Saff"=>array("Physics" => 78.8, "Math" => 56.8, "Chemistry" => 59.6),
    "Ottie"=>array("Physics" => 85.9, "Math" => 54.3, "Chemistry" => 67.6)
);

    //Accessing elements of multidimensional array
    echo "Kindson scored ".$scores["Kindson"]["Physics"] . " in Physics <br>";
    echo "Saff scored ".$scores["Saff"]["Math"] . " in Math <br>";
    echo "Ottie scored ".$scores["Ottie"]["Chemistry"] . " in Chemistry <br>";
?>  

 

The output of this program is shown below:

Kindson scored 99.4 in Physics
Saff scored 56.8 in Math
Ottie scored 67.6 in Chemistry