|
ArraysArrays actually act like both hash tables (associative arrays) and indexed arrays (vectors). Single Dimension ArraysPHP supports both scalar and associative arrays. In fact, there is no difference between the two. You can create an array using the list() or array() functions, or you can explicitly set each array element value.
You can also create an array by simply adding values to the array. When you assign a value to an array variable using empty brackets, the value will be added onto the end of the array.
Arrays may be sorted using the asort(), arsort(), ksort(), rsort(), sort(), uasort(), usort(), and uksort() functions depending on the type of sort you want. You can count the number of items in an array using the count() function. You can traverse an array using next() and prev() functions. Another common way to traverse an array is to use the each() function. Multi-Dimensional ArraysMulti-dimensional arrays are actually pretty simple. For each dimension of the array, you add another [key] value to the end:
In PHP3 it is not possible to reference multidimensional arrays directly within strings. For instance, the following will not have the desired result:
In PHP4, however, the whole problem may be circumvented by enclosing the array reference (inside the string) in curly braces:
You can "fill up" multi-dimensional arrays in many ways, but the trickiest one to understand is how to use the array() command for associative arrays. These two snippets of code fill up the one-dimensional array in the same way:
The array() function can be nested for multi-dimensional arrays:
|
|||||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |