Search This Blog

Monday, September 13, 2010

Programming PHP Arrays

The most inconvenient thing about a variable in software programming is that you can only store one value at a time. Arrays are special types that allow variables to overcome this limitation, so you can store as many values as you want in the same variable. For example, instead of having two variables "$number1" and "$number2", you could have an array "$numbers" that will hold both values. Imagine the same thing with ten numbers. What about a hundred? Because of the flexibility of the array, it can store two values or two hundred values, without having to define other variables. The PHP server-side scripting language indexes all the values within an array using a number or a string, so you will know which of the values you're using.
Programming with arrays is easy. You can process each item one after another, or you could just take one at random. Each item in an array is commonly referred to as an element. These elements can be accessed directly via their index, which can be either a number or a string. By default, PHP starts indexing elements numerically, from zero, and increments the element's index with each new addition, so keep in mind that the index of the last elements in a numerically indexed array is always the total number of elements minus one. Indexing arrays by string can be useful in cases where you need to store both names and values.
There are two ways you can create an array: with the "array()" function or directly using the array identifier "[]". You can use the "array()" function when you want to assign multiple elements to an array at a time. Don't think that an array can only contain elements of a certain type (for example, only numbers). You can have numbers, strings and booleans in the same array, PHP won't mind at all.
$names = array("John", 279, "Betty",TRUE);
This creates an array called "$names" which holds the specified elements. You can access an array element by placing its index between square brackets, right after the array name. Not only you can retrieve a value this way, but you can also assign a value to that element.
print $names[2]; //outputs "Betty"
$names[3] = "Harrison"; //replaces TRUE with "Harrison"
Remember that PHP starts indexing from zero, therefore "$names[0]" is "John", and so on – the index of any element always is one less than the element's place in the list. Another way to define an array is using the array identifier "[]" in conjunction with the array name. You can also use this to add new elements in you have already created an array – either using the "array()" function or the array identifier.
$names[] = "John";
$names[] = "Mary";
$names[] = "Betty";
$names = array("Harry", "Samantha","Danny");
There is no need to place any numbers between the square brackets, PHP takes care of the index number, so you don't have to figure out which is the next available slot. This doesn't mean that you cannot add numbers, but pay attention not to skip any of them, because PHP will initialize only the elements with the index number you specify.
Arrays indexed by strings, and not numbers, can prove to be useful when you need to access elements in array by name, and not by number. For example, if you have an address book, it would be much better to have a field called "name" or "address", instead of a numeric field called "1" or "2". You can define an associative array pretty much the same way you define a numerically indexed array.
$sysinfo = array(computer_name => "My computer",
cpu_mhz => 2800,
memory_size => 512,
multimedia => TRUE);
Or you can use the array identifier:
$sysinfo[computer_name] = "My computer";
$sysinfo[cpu_mhz] = 2800;
$sysinfo[memory_size] = 512;
$sysinfo[multimedia] = TRUE;
It's good to know that an array element can itself be an array, so this enabled you to create sophisticated data structures called multidimensional arrays. Let's say you have an address book, and you use an array to hold the information on the people you know (John, Mary, Betty, Harry). But on the other hand, each element must be a collection of a person's details (first name, last name, telephone number). This is how you define it:
$address_book = array(
array(first_name => "John", last_name => "Davis", phone_number => "1234567"),
array(first_name => "Mary", last_name => "Stewart", phone_number => "1234568"),
array(first_name => "Betty", last_name => "Willis", phone_number => "1234569"),
array(first_name => "Harry", last_name => "Miller", phone_number => "1234560"));
You can access the data using two indices, the first one for "$address_book" – which is a numeric index, and the second one for the person's details – which is a string index. The following text outputs the text "Mary":
print $address_book[1][first_name];
There are a lot of functions to use with arrays. You can merge, slice, shift and sort arrays, by using the functions with the same name: "array_merge()", "array_slice()", "array_shift()", and "sort()". Sorting is definitely one of the most used functions. The "sort()" function can sort both alphabetically or numerically, depending on the array elements. Read the PHP documentation to learn how to use these functions.

No comments:

Post a Comment