PHP 7.x — P9: Simple Arrays. What is an array? In layman's terms…
By: Dino Cajic
Tags: Computer Science, Programming, PHP, Software Development, Web Development

What is an array? In layman's terms, it’s a way to store multiple items together under one variable. You could create different variables for everything, but sometimes that’s just not practical. Imagine that you’re pulling thousands of records from a database. You can’t, or shouldn’t, create thousands of different variables to store each record. You would use an array and store all of the records under one variable. You could then loop through the array (I know we haven’t covered loops yet) and display each array element.

There are different ways to create an array in PHP. The first way is by assigning array() to a variable. Inside the array() declaration, we’ll put a list of numbers.

<?php

$numbers = array(1, 2, 3, 5, 10, 20);

?>

Those numbers could serve a purpose, such as represent employee ID’s.

<?php

$employee_ids = array(1, 2, 3, 5, 10, 20);

?>

Arrays don’t have to store just integer values; they can store any data type including, for example, strings.

<?php

$drivers_i_hate = array(

'Drivers that turn on their signals before checking',

'Drivers that do the speed limit in the left two lanes',

'Drivers that are clearly on their phones'

);

?>

You’ll notice that each array value is on a separate line. PHP doesn’t have a limit on how many spaces or new line characters you can have after each array element. So, for readability, you can make it look however you want.

How do we access elements inside the array? With the index value. What is an index? It’s just an integer value that’s assigned to each element so that it can be called. In PHP, like most programming languages, we start at index 0 and move up in increments of 1. Why is 0 the first array index? So as not to get too deep into it, I’ll briefly state that it’s because of the way that arrays are stored in memory.

In the $drivers_i_hate array, we have 3 elements; the index values are 0, 1, and 2. To access the first array element, you’ll take your variable, $drivers_i_hate, append a couple of brackets to it, [ ], and place the index value in between the brackets.

<?php

echo $drivers_i_hate[0];

// Displays: Drivers that turn on their signals before checking

?>

To display the last element, we could just count the number of elements in there, 3, subtract 1, since our index value starts at zero, and place the integer value inside the square brackets.

<?php

echo $drivers_i_hate[2];

// Displays: Drivers that are clearly on their phones

?>

What if we didn’t know how many elements are in the array? For example, if we were pulling records from a database, and that database table kept getting updated frequently, the number of records would change. Now, what if you wanted to access the last element. You would not be able to hard-code the index value since it changes. We’ll somehow have to count the number of elements in the array and figure out the value that way.

Luckily, PHP has a built-in function called count(). There is another function called sizeof(), which is synonymous to count(). You can choose whichever one you like; I prefer count.

Lets pass the array to the count() function and get the number of elements inside the array. We get 3 since that is the number of elements inside the $drivers_i_hate array.

<?php

echo count( $drivers_i_hate );

// Displays: 3

?>

With the information that we know so far, let’s see if we can access the last array element. All we have to do is get the number of elements inside the array, subtract 1 from it since the index starts at 0, and place the value in between the square brackets of the array.

<?php

// Stores the value 2

$last_index = count($drivers_i_hate) - 1; echo $drivers_i_hate[ $last_index ];

?>

If we wanted to, we could just pass the entire expression in between the square brackets; we don’t have to store the index value inside of a variable.

<?php

echo $drivers_i_hate[ count($drivers_i_hate) - 1 ];

?>

How does PHP evaluate the code above?