25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Arrays ” 67<br />

$keys = array_rand ($cards, 2);<br />

var_dump ($keys);<br />

var_dump ($cards);<br />

If you run the script above, its output will look something like this:<br />

array(2) {<br />

[0]=><br />

string(1) "a"<br />

[1]=><br />

string(1) "b"<br />

}<br />

array(3) {<br />

["a"]=><br />

int(10)<br />

["b"]=><br />

int(12)<br />

["c"]=><br />

int(13)<br />

}<br />

As you can see, extracting the keys from the array does not remove the corresponding<br />

element from it—something you will have to do manually if you don’t want to extract<br />

the same key more than once.<br />

Arrays as Stacks, Queues and Sets<br />

Arrays are often used as stacks (Last In, First Out, or LIFO) and queue (First In, First<br />

Out, or FIFO) structures. <strong>PHP</strong> simplifies this approach by providing a set of functions<br />

can be used to push and pop (for stacks) and shift and unshift (for queues) elements<br />

from an array.<br />

We’ll take a look at stacks first:<br />

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

$stack = array();<br />

array_push($stack, ’bar’, ’baz’);<br />

var_dump($stack);

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

Saved successfully!

Ooh no, something went wrong!