This section introduces these functions and offers several
examples.
?– Note A traditional queue is a data structure in which the elements are removed in the same order in
which they were entered, known as first-in-first-out, or FIFO. In contrast, a stack is a data structure in
which the elements are removed in the order opposite to that in which they were entered, known as lastin-
first-out, or LIFO.
CHAPTER 5 ?– ARRAYS 135
Adding a Value to the Front of an Array
The array_unshift() function adds elements onto the front of the array. All preexisting
numerical keys are modified to reflect their new position in the array, but
associative keys aren??™t affected. Its prototype follows:
int array_unshift(array array, mixed variable [, mixed variable...])
The following example adds two states to the front of the $states array:
$states = array("Ohio","New York");
array_unshift($states,"California","Texas");
// $states = array("California","Texas","Ohio","New York");
Adding a Value onto the End of an Array
The array_push() function adds a value onto the end of an array, returning TRUE on
success and FALSE otherwise. You can push multiple variables onto the array simultaneously
by passing these variables into the function as input parameters. Its prototype
follows:
int array_push(array array, mixed variable [, mixed variable...])
The following example adds two more states onto the $states array:
$states = array("Ohio","New York");
array_push($states,"California","Texas");
// $states = array("Ohio","New York","California","Texas");
Removing a Value from the Front of an Array
The array_shift() function removes and returns the item found in an array.
Pages:
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217