Arrays

An array is a special type of variable that can act like a list or like a table in a spreadsheet program. Because of their power and flexibility, arrays are widely used in advanced PHP programming. I'll run through a couple of the strongest array functions now.

New to PHP 4 is the foreach construct, designed to more easily access all of an array's keys and values. Assuming you have an array of name $array, you can access every element using the following code:

foreach ($array as $key => $value) { 
   // Code
}

I tend to use foreach when dealing with arrays and will do so in this book. If you are running an earlier version of PHP, you'll need to rewrite the above code as:

 reset ($array); while (list($key, $value) = each ($array)) { // Code ...

Get PHP Advanced for the World Wide Web: Visual QuickPro Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.