Learning PHP – Part two; Arrays
May 26th, 2009 by admin | Filed under Learning PHP.Here’s my follow up on my PHP articles, this time I am going to talk a bit about arrays. They are so important that you should learn about them sooner than later.
What are arrays?
This quote is from wikipedia.org
An array is a systematic arrangement of objects, usually in rows and columns. Specifically, it may refer to several things.
That’s exactly what it is, it’s a variable (see previous post) containing several values (like a shelf with several compartments) that can be retrieved by a key (the number on the compartment). Array keys are per default a number, but it can also be an associative array with text strings for keys.
So make an array and fill it with data
Here is an example on how to declare an array:
<?php
$bookshelf = array(
‘old books’,
‘new books’,
‘cooking books’
);
?>
The variable name is $bookshelf, it is set to be an array(). The contents is put in between the (), but you can also declare an empty array by doing this:
$bookshelf = array();
By default, the first value will have the key 0, the second will have 1, etc.
To refer to a value within an array and echo it out to display:
<?php
$bookshelf = array(
‘old books’,
‘new books’,
‘cooking books’
);echo $bookshelf[0];
?>
The result outputted from this script will be:
old books
By using the [ and ] signs behind a variable you are referring to it as an array, what you put between them is the key you are using. Try to change the value between them to 1 and check the result.
Adding values to a PHP array
You can make use of the array_push() function to insert more variables to a previously declared array.
<?php
$bookshelf = array(
‘old books’,
‘new books’,
‘cooking books’
);array_push($bookshelf, ‘dvds’);
echo $bookshelf[3];
?>
You can also set a variable directly by using the key
<?php
$bookshelf = array(
‘old books’,
‘new books’,
‘cooking books’
);
$bookshelf[3] = ‘dvds’;
echo $bookshelf[3];
?>
Deleting a value from a PHP array
Several ways to this also, you can use array_pop() to unset the last value in the array.
The unset() function can be on the array value to unset it.
You can set it to null by referring to the key
array_pop($bookshelf);
unset($bookshelf[3]);
$bookshelf[3] = null;
While these are regular arrays, there are other possible ways to use this. A string variable like $name = “Espen”; in my previous article is actually an array of characters. $name[0] will refer to ‘E’, 1 will refer to ’s’ so on and so forth.
Associative Arrays in PHP
You can use keys which are not numbers in an array, those are called associative arrays.
Here is an example, I bet you understand most of the basics now, and in case NOT – drop me a comment!
$bookshelf['favourite'] = ‘Marketing 911′;
echo $bookshelf['favourite'];
In my next article you will get magic behind the array variables $_GET, $_POST and $_SERVER to finally be able to handle user input from forms and every secret of the request. Want to know when I post it? Get Learning by RSS.

Follow me on Twitter
[...] (3); Reserved vars and loops May 28th, 2009 by admin | Filed under Learning PHP. In my previous article I explained arrays and their use. But have you ever wondered what happens to those variables set in [...]