Chapter 6. PHP Arrays
In Chapter 3, I gave a very brief introduction to PHPâs arraysâjust enough for a little taste of their power. In this chapter, Iâll show you many more things that you can do with arrays, some of whichâif you have ever used a strongly typed language such as Câmay surprise you with their elegance and simplicity.
Not only do arrays remove the tedium of writing code to deal with complicated data structures, but they also provide numerous ways to access data while remaining amazingly fast.
Basic Access
Weâve already looked at arrays as if they were clusters of matchboxes glued together. Another way to think of an array is like a string of beads, with the beads representing variables that can be numbers, strings, or even other arrays. They are like bead strings because each element has its own location and (with the exception of the first and last ones) each has other elements on either side.
Some arrays are referenced by numeric indexes; others allow alphanumeric identifiers. Built-in functions let you sort them, add or remove sections, and walk through them to handle each item through a special kind of loop. And by placing one or more arrays inside another, you can create arrays of two, three, or any number of dimensions.
Numerically Indexed Arrays
Letâs assume that youâve been tasked with creating a simple website for a local office supply company and youâre currently working on the section devoted to paper. One way to manage the various ...
Get Learning PHP, MySQL & JavaScript, 6th Edition 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.