PHP Tutorial

? ? ?

What is an array, how to add values to it?

An array is a special type of variable, that can store multiple values at a time.

It can be very useful when we want to store in a single place, a list of similar values.

For example we may want to store a list of companies, without arrays we would need to have a new variable for each company.


//creating a variable for each value
$company_a = 'Tesla';
$company_b = 'Google';
$company_c = 'Nike';
$company_d = 'Amazon';

An easier and cleaner alternative for the code written above would be by using arrays. To create an array we can either use the statement array() or the shorter version [], they both do the same thing.


//creating an array for all values
$companies = array('Tesla', 'Google', 'Nike', 'Amazon');

You can create an array with its values already inside it or you can add them later.


//creating an empty array
$cars = []; 
//same as '$cars = array()' 

//adding values to the array
$cars[] = 'Tesla';
$cars[] = 'BMW';
$cars[] = 'Toyota';

Based on array's structure, there are three types of arrays:

  • Indexed arrays - the arrays have a numeric index for each value;
  • Associative arrays -   the arrays have a string index for each value;
  • Multidimensional arrays - the arrays have inside them one or more arrays. 

Depending on how we want to store data and later access it back, we can choose to use a different type of array.

How you create a new empty array?

What is an indexed array in PHP?

An indexed array stores each value paired with a numeric key, named index.

By default, PHP will assigned an index to each value (if we don't assign manually an index) in a linear fashion (like 0, 1, 2, 3 ...). It's important to remember that automatically the system gives to the first value the index '0' (doesn't starts with '1').

To access a specific value we can simply specify the index of the value we want retrieved, like in the example bellow:


// PHP will assign automatically an index(starting with 0) to each value
$coffee = ['Long Black', 'Cappuccino', 'Espresso', 'Macchiato', 'Mochaccino'];

// using print_r PHP will output the values of the array

PHP output:
    [0] => Long Black
    [1] => Cappuccino
    [2] => Espresso
    [3] => Macchiato
    [4] => Mochaccino

// output a specific value from the array
echo 'A very popular coffee is '.$coffee[1].'.';
// will output 'A very popular coffee is Cappuccino.'

// assign the value to a variable
$selected_coffee = $coffee[2];

We can also assign manually an index to each value, when we add it to the array, like in the example bellow.


we assigned manually the index values,
even though we assigned the values in a random way
it would make it easier to retrieve the values if they in a ordered way

$main_coffee_types = [4 => 'Long Black', 7 => 'Cappuccino', 8 => 'Espresso', 11 => 'Mochaccino']

// assigning the values one by one
$types_of_cappuccino[0] = 'Traditional Cappuccino';
$types_of_cappuccino[1] = 'Iced Cappuccino';
$types_of_cappuccino[2] = 'Dry Cappuccino';
$types_of_cappuccino[3] = 'Wet Cappuccino';

What is the index of the value 'Ginger', from the array bellow?


$spices = array('Basil', 'Ginger', 'Turmeric');

What is an associative array in PHP?

An associative array is similar to an indexed array. The difference is that instead of using numeric keys for each value, we need to assign named keys (strings) for each value. We will need specify the value of each, they are not assigned automatically.

We can create an associative array as in the examples bellow:

  • by declaring an array with the values already inside it  

$food_calories = ['bread' => 265, 'fries' => 312, 'apple' => 52, 'yogurt' => 59];
  • by adding the values later

$food_colour = [];
$food_colour['strawberries']  = 'red';
$food_colour['bananas']       = 'yellow';
$food_colour['watermelons']   = 'green';

We then can later access the values of the array by specifing the key of the value we want, as in the example.


//the bottom example will output for $food_colour['strawberries'] the value 'red'
echo 'The strawberries are '.$food_colour['strawberries'].'.';

What type of data is used for the keys of an associative array?