Chapter 8. Learning Arrays

Arrays are definitely one of the coolest and most flexible features of PHP. Unlike vector arrays from other languages (C, C++, Pascal), PHP arrays can store data of varied types and automatically organize it for you in a large variety of ways.

Note

This chapter treats arrays and array functions in some depth. For a very quick introduction to the syntax and use of arrays, see Chapter 4. For a more complete survey of advanced array functions, see Chapter 21.

The Uses of Arrays

An array is a collection of variables indexed and bundled into a single, easily referenced supervariable that offers an easy way to pass multiple values between lines of code, functions, and even pages. Throughout much of this chapter, we will be looking at the inner workings of arrays and exploring all the built-in PHP functions that manipulate them. Before we get too deep into that, however, it's worth listing the common ways that arrays are used in real PHP code.

Many built-in PHP environment variables are in the form of arrays (for example, $_SESSION, which contains all the variable names and values being propagated from page to page via PHP's session mechanism). If you want access to them, you need to understand, at a minimum, how to reference arrays.

Almost any situation that calls for a number of pieces of data to be packaged and handled as one is appropriate for a PHP array.

What Are ...

Get PHP6 and MySQL® 6 Bible 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.