An Introduction to Arrays

An Introduction to Arrays
Difficulty: Easy
Description: Explains how to store variables in an array.
Author: M@ster; Date Added: 2008-08-24; Views: 6285
An array is basically a storage device which holds items and then later you can extract what you want from it. Arrays are also great for cleaning up your sql database and make it easier to get and store information.

Here is an example of an array:
$arr = array(
     "0" => "Welcome",
     "1" => "to",
     "2" => "Gamexe.net"
);
As you can tell by the $arr this is a variable containing an array that contains more variables.If you notice the comma (,) after "Welcome", and "to", that basically means your starting a new line; almost like a line break. If you also notice after "Gamexe.net" there is no comma thats because we don't have another line/row after that. If you where to add the comma on the last variable/line it would give you an error.

Getting information out of an array.[

Using the same array above I will now explain how to extract information from it. If you notice the "o" all the way through "2" those are variables, so if we want to extract them we would simply do this:
echo $arr[o];
The output of this would be:

Welcome
echo $arr[o];  //output: Welcome
echo $arr[1];  //output: to
echo $arr[2];  //output: Gamexe.net
Then if I did this:
echo "$arr[0]"." $arr[1]"." $arr[2]";
This would output:

Welcome to Gamexe.net

Storing arrays in a mysql database?

Well you may wonder how you store arrays in a mysql database and luckily for you it only takes one more line of code. You simply have to serialize your array then you can place it in your database.

Example:
$arr = array(
     "0" => "Welcome",
     "1" => "to",
     "2" => "Gamexe.net"
);

$serialize = serialize($arr);
By serializing your array it would look like:
a:3:{i:0;s:7:"Welcome";i:1;s:2:"to";i:2;s:10:"Gamexe.net";}
And then you can place it in your database. Although if you want to extract information out of it after grabbing it from the database you have to unserialize it first.
$unser = unserialize($arr);

Hope this tutorial helped!

-Ryan