Saturday, April 3, 2010

Array

An array is a special variable, which can store multiple values in one single variable. There are 3 kind of arrays in PHP:
  • numeric - An array with a numeric index.
  • associative - An array where each ID key is associated with a value.
  • multidimensional - An array containing 1 or more arrays.



Numeric Array

There are 2 methods to create a numeric array. The following example shows the index are automatically assigned (the index starts at 0):


(click image to view)

And the other one, we assign the index manually:


(click image to view)

Lets try running this code:


(click image to view)

############################################################
The Output:

Modenas and Naza are Malaysia motorcycles
############################################################



Associative Array

It is not the best way for you to use numerical array to store data about specific named. However, if we use associative array, we can use the values as keys and assign values to them. For instance:


(click image to view)

############################################################
The Output:

Kathy's weight is 45 kilograms.
############################################################



Multidimensional Array

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Look at the below:


(click image to view)

############################################################
The Output:

Is Lawrence a part of the Gilbert family?
############################################################

No comments:

Post a Comment