Pin Me

A Guide to Linux Programming with PHP - Arrays

written by: Berry van der Linden•edited by: Lamar Stonecypher•updated: 9/2/2009

Understanding arrays is an important step in programming. At first, the concept of an array is not easy to get your head around - An array can have lots of elements and have many different uses. If you want to code, you need to learn about arrays and this article will get you started!

  • slide 1 of 4

    Introduction to Arrays

    Now that you are amazing with variables, the next best thing for you to learn are arrays. Unlike variables, that store one value, arrays allow you to store lots of values in a variable - Clever stuff!

    Arrays are sort of similar to a list, however, a list can only be used store items. Arrays are a container where you can store, sort and group objects. The main difference between an array and list is that an array (container) holds the objects and a list just references them.

    There are two main types of array: numeric and associative. There is also a third called a multi-dimensional array. PHP treats each of these as the same thing, however, we will learn the concepts of each separately; starting with numerical arrays.

  • slide 2 of 4

    Working with Arrays

    If I wanted to store all of my music artists and songs, I could create each one as a normal variable, which looks like this:

    $artist1 = "Muse";

    $song1 = "New Born";

    $song2 = "Sunburn";

    However, we could also store these in one variable - an array - which shall be named $music. Elements in an array have a unique key, which is essentially an address, so that the elements in the array can be accessed.

    Because there will be several elements, we need to be able to access each one individually. An array will do this for you by numbering them, however, we can choose their names.

    Now we'll create a basic array and put it to some use. Use the array() function to create an array and insert the elements:

    $music = array ( "Muse", "New Born", "Sunburn" );

    This keeps all of my music tastes inside an array $music and gives each element a key comprising of integers. In PHP this starts and 0 and increments by 1 for each element. Muse is element [0], New Born is [1] and Sunburn is [2].

    You can access any of the array elements by the array name and the element number in square brackets, for example: $music[0]. Here it is in action:


    echo "$music[1]"


    This will echo the second array element, "New Born". In PHP numbers start at zero (0), therefore, $music[1] is second.

    You can also name each element individually when creating or adding to the array:

    $music[] = "Muse";

    $music[] = "New Born";

    $music[] = "Sunburn";

    This is the same as if you just used the array() function. Wow! What is that beautiful noise licking my ears!? It turns out I now have another favourite song. No problem, no matter what the size of the array is, I can simply add the new song to the array:

    $music[] = "Uprising";

    PHP is a clever chap and it will provide "Uprising" with the next available element in the array. Which is [3].

  • slide 3 of 4


    Just so you are clear, we can create an array in this way:

    $music[] = "Muse";

    $music[] = "New Born";

    $music[] = "Sunburn";

    $music[] = "Uprising";

    Or we can do it like this:

    $music = array ( "Muse", "New Born", "Sunburn", "Uprising" );

    PHP will create the addresses of each element for both examples.

    $music[0] = "Muse";

    $music[1] = "New Born";

    $music[2] = "Sunburn";

    $music[3] = "Uprising";

    Again, in both cases, you can select an element by number:


    echo "$music[3]";


    This will output the element "Uprising" as a string.

    There is lots that can be done with an array! You can increment, sort, print, and even use arrays with loops!

  • slide 4 of 4

    Next Time

    Now that you have an idea how simple arrays work in PHP, next time we will take a look at associative arrays. This is where a key is associated with a value. If you wanted to store the test scores of students in an array, an array that is indexed by numbers might not be suitable. Alternatively, we could make keys in our associative array from the student's names and use their score as the value.

    I'll let you think that over before we get to work on associative arrays in the next "A Guide to Linux Programming with PHP".

A Guide to Linux Programming with PHP

In this series of articles I'll be teaching you the basics of using PHP, and showing you why I think it's the best language for web development. There are no pre-requisites for your level of knowledge, other than that you can understand basic HTML and are willing to learn.
  1. A Guide to Linux Programming with PHP
  2. A Guide to Linux Programming with PHP - Commenting and Executing Code
  3. A Guide to Linux Programming with PHP - Variables & Data Types
  4. A Guide to Linux Programming with PHP - HTML Forms and PHP
  5. A Guide to Linux Programming with PHP - Arrays
  6. A Guide to Linux Programming with PHP - Associative and Multidimensional Arrays