Monday, February 4, 2008

ARRAYS

Good Morning Everyone.Am sure you have been practicing on our last tutorial.We talked about; -Installing Necessary applications or softwares to make our lesson easier.Those software are PHP interpreter,MYSQL Database and Apache server. Today,we will be looking at ARRAYS. Arrays are group of items sharing the same characteristics such as CAR MODELS,BASEBALL TEAM and TYPES OF HOUSES.Basically its used to group many variables and stored in to another variable.

CREATING ARRAYS

Arrays can be created like this

$names[0]="Peter"; // Numerical key
$names = array (0 => "julie", 1 => "peter",2 => "mark"); //Numerical key
$names["Friend"]="Sammy"; //words as index key.


You can see from the above examples the various ways arrays are created.The first one has a numerical key which can be referenced like this

echo $names[0];

While,the second one can be referenced, the same way as the first.But the only difference between the first two and the third one,that is the one with a word as its key,is that instead of enclosing 0 in the bracket,we gave it a name which is Friend,so we call it like this

echo $names["Friend"];


As easy as ABC,if you run this,you will get Sammy as your result..

ARRAY()

We can also create arrays with this function.

$languages=array("English","Frenceh","Spanish");


Alternatively,This function can be used to create Assoiciative arrays.An associative array bears a relationship with the value itself.Now lets say we want to specifie relationships to the elements in our previous example.

$names = array ("person1"=> "julie","person2" => "peter","person3" => "mark");

Now you see the difference,we removed the number indexes and changed them to strings.Therefore,the key to julie is "sister".Te reference it is so easy.

echo $names["person1"];. "is". $names["person2"]; ."sister and" . $names["person3"]."bestfriend";

RESULT:Julie is peter sister and mark bestfriend

Now,isnt that easy and interesting?Also,it is very possible to create and array inside an array and it is called a multidimensional Array.For example,Lets say we decided to include the sex,ages of peter,julie and mark in our example,we will do it this way

$names=array("person1"=>array("Age"=>"22","Sex"=>"F"),"person2"=>array ("Age"=>"27","Sex"=>"M "),"person3"=>array("Age"=>"25","Sex"=>"M"));

What we did here is to create another aray in an array,so this is how we reference a multi-dimensional array.


echo $names["person2"]["Age"]; #Result 27

This will display 27 as result.There are so many other ways of outputing array elements.PHP as a large language contains some functions which we will learn.Hope you are having fun!cause i am.

3 comments:

cobra90nj said...

complimets for your blog ;)

FootBall Arena said...

Thanks cobra90nj.This is the first comment i have recieved.Very encouraging Help spread and contribute.

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Design by Dzelque Blogger Templates 2007-2008