PHP Tutorial – Arrays

In this PHP language tutorial we will take a look at arrays in PHP. A normal variable holds only one value. An array is different. An array lets you declare and work with a collection of values.

If you would use normal variables to store for instance a list of names it would look something like this:


     $name=”John”;
     $name2=”Karin”;
     $name3=”Paul”;
      ….
     $name1000=”Anne”;

Of course you can do this, but it’s much easier to use an array for this. An array can hold all variables values under one single name. Each value is stored in an array element. You can access the values by referring to the array name and a specific element.

In the PHP language there are three kind of arrays:

  • Numeric array – This array type has a numeric index (each element has a number.)
  • Associative array – This array type uses an ID key to associate with a value.
  • Multidimensional array – This array type has rows and columns (the previous two are one-dimensional array types that have one column of elements.)

Numeric Arrays

As said before, a numeric array uses a numeric index (a number) to point to a specific element. Note: the first element of an array start at 0!

Let’s look at an example:


      $name=array(“John”.”Karin”,”Paul”);

In this case the array index is automatically build (starting with zero as the first element in the array.) Of course you could do the same manually than it would look like this:


     $name[0]=”John”;
     $name[1]=”Karin”;
     $name[2]=”Paul”;

Both array statement produces the same array called “name” with 3 elements with the values “John, Karin, Paul”.
Let’s look at an example where we will echo the values of an array:


<?php
$name=array(“John”.”Karin”,”Paul”);
echo $name[0] . “and”  . $name[2]. “are boys names.”;
?>

Output of the example:


     John and Paul are boys names.

Associative Arrays

An associative array uses an ID key to associate with a value. You can use this array type for instance when you store data about specific named values. Of you could use a numerical array for this, but it’s easier to use an associative array.

Let’s us make an example; we have a number of people’s names and the age of each person.


    John  = 16 years old
    Karin = 25 years old
    Paul = 40 years old

We could have used two numerical arrays to associate the person’s name with the age, but an associative array is much easier.


<?php
     $name = array(“John”=>16, “Karin”=>25, “John”=>40);
     echo “John is “ . $name[‘John’] .”years old and the rest is older.”;
?>

You can see that this is much easier to understand than using a numeric number. You just say; I want the age of Karin and you get the age of Karin. (Instead of two numeric arrays where in the first array you put all the names and in the second all the ages.) You can do it like this, but we hope that you see that associative arrays are much easier and more natural.

Multidimensional Arrays

This array type has rows and columns (the previous two are one-dimensional array types that have one column of elements.) In other words each element in the array can also be an array and each element in the sub-array can be an array, etc.

Sounds confusing doesn’t it, so let’s look at an example:


     $families = array(“Potter”=>array(“John”,”Karin”,”Paul”));

The main array gets the name “families”. The sub-array gets the name “Potter” with values “John, Karin, Paul” in element 0, 1, 2.

If we want to add another family called “Johnsens” we can simple add a sub-array, like so:


<?php
     $families = array(
          “Potter”=>array(“John”,”Karin”,”Paul”),
          “Johnsens”=>array(“Anne”, “Glenn”)
     );

     echo “The family Johnsens have two members; “ 
           . $families['Johnsens'][0] . “and ” . $families['Johnsens'][1] ;
?>

That’s all for this tutorial.

This entry was posted in PHP Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “PHP Tutorial – Arrays”

Why not let us know what you think by adding your own comment!

  1. Muhammad Abid on December 14th, 2010:

    Not bad

  2. avnish singh on August 16th, 2012:

    thanks for tutorial…

Leave a Reply: