Tuesday, June 26, 2012

PHP Learning - Working with Array



Starting from today, we are going to work with arrays.
Arrays are complex variables that store a group of values under a single
variable name. An array is useful for storing a group of related values.
For example, you can store information about a computer, such as computer case, CPU, memory, and cost, in a single array named $Mycomputerinfo. Information in an array can be handled, accessed, and modified easily.
First, let's take a look at how to create arrays.
Just like createing variables, to create an array, first, we need to assign values to an array. Let's say I have a plan for studying different subject, then:
$studying_day[1]="english";
$studying_day[2]="french";
$study_day[3]="math";
$study_day[4]="business";
......
if you are crazy about studying, then you can creat along list. We observe the general formating for array is:
$arrayname[key]="value";
Array can use either numbers or strings for keys. If you don't assign any value to the key, for example, if I use $studying_day[]="english"; then the key will automatically assigned to a value of zero.
We can also use a shortcut for the above array. $studying_day(1=>"english","french","math","business");
You can also create an array with a range of values by using the following
statement:
$years = range(2001, 2010); the result looks like the following:
$years[0] = 2001
$years[1] = 2002
. . .
$years[8] = 2009
$years[9] = 2010
In my next php learning lesson, I am going to talk about how to view arrays.
View the original article here

No comments:

Post a Comment