Intro to Arrays in PHP
August 26, 2005 | 4 Comments
PHP has the ability to use associative arrays. These are by far the most useful and versatile data structure in PHP (and debatably any language). Instead of using the more common indexed array, where integers are used to identify the elements of the array, you can use strings.
If you are writing PHP, you have to know associative arrays and feel good about them.
Here's an example showing some common uses:
Associative arrays are also the most readable and convenient way to deal with database results. Suddenly you have an array that is indexed by field name from the table. How could anything be cooler than that?
For debugging arrays, there is nothing more useful than print_r():
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
If you want to add an element to the end of an array you can use the [] notation (sort of like a stack push):
Output:
Array
(
[0] => Monkey
[1] => Alligator
[2] => Rabbit
)
Read More about PHP arrays on PHP.net.
Tags: array, arrays, associative, associative array, example, introduction, php, PHP.net, programming
