1. Computing

Understanding Arrays in PHP

By

You have probably heard the term array a lot but you might not actually understand what an array is. Today we are going to attempt to clarify what an array is, and how you might use an array in one of your PHP programs.

An array is a systemic arrangement of objects. Hum, what does this mean? Well in programming an array is a type of data structure. Each array can hold several pieces of information. It’s sort of like a variable in that it stores data, but not at all like a variable in that instead of storing one bit of information it can store many pieces of information.

Let’s start with an example... let’s say that you are storing information about people. You could have a variable that stored my name “Angela”. But in an array you could store my name, my age, my height, my eye color, and my favorite food.

Let’s look at some sample code. In this sample code we will look at storing two bits of information at a time, the first being somebody’s name and the second being their favorite color.

<?php 
 
$friend[0] = "Kevin"; 
 $friend[1] = "Bradley”; 
 $friend[2] = "Alexa"; 
 $friend[3] = "Devin";

$color["Kevin"] = “Teal”; 
 $color["Bradley"] = “Red”; 
 $color["Alexa"] = “Pink”; 
 $color["Devin"] = “Red”;

print "My friends names are " . $friend[0] . ", " . $friend[1] . ", " . $friend[2] . ", and " . $friend[3];

print "<p>";

print "Alexa ‘s favorite color is " . $color["Alexa"] . "."; 
 ?>

In this example code you can see that the friend array is sorted by number, and contains a list of friends. In the second array, color, instead of using numbers it uses strings to identify the different bits of information.

The identifier used to retrieve data from the array is called it’s key. In our first example the keys were integers 0, 1, 2, and 3. In our second example the keys were strings. In both cases we are able to access the data held in the array by using both the array’s name, and the key.

Like variables, arrays always start with a dollar sign ($array) and they are case sensative. They can not start with an underscore or a number, you must start them with a letter.

So now you sort of get what an array is, its like a variable with lots of little variables inside of it... but what exactly do you do with an array... how is it useful to you as a PHP programmer?

From a strictly making an array standpoint, it’s not. You will probably never create an array like the one in the example above. The most useful thing you can do with an array in PHP is to use it to hold information you get form somewhere else.

Having your website's information stored in an MySQL database is not uncommon. When your website needs certain information it simply accesses your database, and wha-laa, on demand data.

Let’s say you have a database of people who live in your city. You now want to search that database and print out records for anyone named “Tom”. How would you go about doing this?

Basically you would read through the database for people named Tom, and then pull their name and all the other information about them from the database, and place it in an array inside of your program. You are then able to cycle through this array, and print out the information or store it to use elsewhere in your program.

A good example of how to write data from a MySQL database to an array to be used in your program can be found here.

To conclude: on the surface an array might not look that interesting to you, but when you do more programming and start storing more complex data structures you will find you are often writing them to arrays when they need to be used.

  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. Practical uses for Arrays in PHP

©2014 About.com. All rights reserved.