Student Of Fortune

Fundamentals of PHP - Array

Share on :
Studying the array is very important in making the program because it greatly increases the flexibility of a variable rate, that's why I call it an additional weapon in the use of variables (which you already know in part?). Example you can do with arrays is to transpose the data into a form that we want, and manipulate data from databases, etc.. Interested? Please read more ....

If I want to make a list containing the names of all the animals in my house, I can insert each into a separate variable. For example, I have 2 tails giraffe named Diana and sely, and a lizard named Sawiyah . We will try to fill each one into a variable:

$ giraffe1 = "Diana";
$ 
giraffe2 = "Bejo";
$ lizard = "Sawiyah";

With array, we can save all the contents of these variables into a variable for example, we give the name $animal. Each elements variable will have a "marker" itself (can contain numbers or letters) used to access certain parts of array.

I will try to explain the concept of "marker" in another way: If we save 3 pieces of different variables into a variable (such as save Diana, Bejo and Sawiyah into the $animal), we need a way to take back a certain part (eg Diana only) to we use next. An array would automatically number each of elements that make up the array, so that the marker can be elements 1, elements 2 and elements 3. Or, as we shall see later, we can call every part of the array using texts. In our case this time, we can mark each elements $animals with the marker "short giraffe, giraffe high" and "lizard" to then using these markers to identify each member of the array.

Well, we try to create a simple array and then use it. The easiest way to producing an array is to using function array (), which will fill our array with the variable we want. Example:

$animals = array ("Diana", "Bejo", "Sawiyah");
This command will save all our animals names into a variable ($animals), and automatically allocate a number of "marker" to each array element in sequence starting from 0 (please note, ranging from 0, to avoid confusion when using array to manipulate our variable later). Thus, Diana is the elements [0], Bejo [1] and Sawiyah [2]. We provide array names as we want ($animals).

now you can take the array that our want to refer to variables so that they name followed by a number of elements in square brackets ($animals [0] will contain Diana). Now we try to look at our array in action:


print "$animals[2]"; 
?> 
                                                                                                                                                              
hopefully useful

0 comments on Fundamentals of PHP - Array :

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank