PHP ArraysAn array can store one or more values in a single variable name. What is an array?When working with PHP, sooner or later, you might want to create many similar variables. Instead of having many similar variables, you can store the data as elements in an array. Each element in the array has its own ID so that it can be easily accessed. There are three different kind of arrays:
Numeric ArraysA numeric array stores each element with a numeric ID key. There are different ways to create a numeric array. Example 1In this example the ID key is automatically assigned:
Example 2In this example we assign the ID key manually:
The ID keys can be used in a script:
The code above will output:
Associative ArraysAn associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them. Example 1In this example we use an array to assign ages to the different persons:
Example 2This example is the same as example 1, but shows a different way of creating the array:
The ID keys can be used in a script:
The code above will output:
Multidimensional ArraysIn 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. ExampleIn this example we create a multidimensional array, with automatically assigned ID keys:
The array above would look like this if written to the output:
Example 2Lets try displaying a single value from the array above:
The code above will output:
|
Tuesday, 14 April 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment