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.

50 ” Arrays<br />

$a = array (’4’ => 5, ’a’ => ’b’);<br />

$a[] = 44; // This will have a key of 5<br />

i<br />

Note that array keys are case-sensitive, but type insensitive. Thus, the key ’A’ is different<br />

from the key ’a’, but the keys ’1’ and 1 are the same. However, the conversion is<br />

only applied if a string key contains the traditional decimal representation of a number;<br />

thus, for example, the key ’01’ is not the same as the key 1.<br />

Multi-dimensional Arrays<br />

Since every element of an array can contain any type of data, the creation of multidimensional<br />

arrays is very simple: to create multi-dimensional arrays, we simply<br />

assign an array as the value for an array element. With <strong>PHP</strong>, we can do this for one or<br />

more elements within any array—thus allowing for infinite levels of nesting.<br />

$array = array();<br />

$array[] = array(<br />

’foo’,<br />

’bar’<br />

);<br />

$array[] = array(<br />

’baz’,<br />

’bat’<br />

);<br />

echo $array[0][1] . $array[1][0];<br />

Our output from this example is barbaz. As you can see, to access multi-dimensional<br />

array elements, we simply “stack” the array operators, giving the key for the specific<br />

element we wish to access in each level.<br />

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

Unravelling Arrays<br />

It is sometimes simpler to work with the values of an array by assigning them to<br />

individual variables. While this can be accomplished by extracting individual ele-

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

Saved successfully!

Ooh no, something went wrong!