25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3<br />

Arrays<br />

Arrays are the undisputed kings of advanced data structures in <strong>PHP</strong>. <strong>PHP</strong> arrays are<br />

extremely flexible—they allow numeric, auto-incremented keys, alphanumeric keys<br />

or a mix of both, and are capable of storing practically any value, including other<br />

arrays. With over seventy functions for manipulating them, arrays can do practically<br />

anything you can possibly imagine—and then some.<br />

Array Basics<br />

All arrays are ordered collections of items, called elements. Each element has a value,<br />

and is identified by a key that is unique to the array it belongs to. As we mentioned<br />

in the previous paragraph, keys can be either integer numbers or strings of arbitrary<br />

length.<br />

Arrays are created one of two ways. The first is by explicitly calling the array()<br />

construct, which can be passed a series of values and, optionally, keys:<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

$a = array (10, 20, 30);<br />

$a = array (’a’ => 10, ’b’ => 20, ’cee’ => 30);<br />

$a = array (5 => 1, 3 => 2, 1 => 3,);<br />

$a = array();<br />

The first line of code above creates an array by only specifying the values of its three<br />

elements. Since every element of an array must also have a key, <strong>PHP</strong> automatically

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!