A variable is limited by the fact that it can only store one value, whether it be a number or text. This limitation is overcome by the use of an array, which allows you to store multiple values. You can access these values by simply referring to the array, or you can also access specific values or elements in the array, since each has its own index. As you use PHP, you may find it useful to sort elements in an array into sequences that differ from PHP's default method (arrays are unsorted lists). This tutorial will show you how to achieve this using the PHP sort() function. When using the sort() function, elements will be assigned new keys that replace any existing keys. If the function is successful, it will return TRUE. If it fails, it will return FALSE. The syntax for the sort() function is as follows: sort (array, sorttype) The array parameter identifies the array to sort and is required. The sorttype identifies the way in which the array values are to be sorted. It is optional. Here are some possible sorttype values: SORT_REGULAR – this is the default sorttype that will treat values as they are. By default, PHP will arrange the elements in an array in alphabetical order. For instance, if you used the code: <?php sort($state_array); You result would be something like: Array To have the array sorted, you would use the sort() function like so: <?php sort($state_array); The preceding code will produce the following output: Array
blog comments powered by Disqus |
|
|
|
|
|
|
|