11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

CHAPTER 5 • ARRAYSwww.it-ebooks.infoAdding and Removing Array Elements<strong>PHP</strong> provides a number of functions for both growing and shrinking an array. Some of these functionsare provided as a convenience to programmers who wish to mimic various queue implementations(FIFO, LIFO, etc.), as reflected by their names (push, pop, shift, and unshift). This section introducesthese 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 theywere entered, known as first-in-first-out or FIFO. In contrast, a stack is a data structure in which the elements areremoved in the order opposite to that in which they were entered, known as last-in-first-out or LIFO.Adding a Value to the Front of an ArrayThe array_unshift() function adds elements to the front of the array. All preexisting numerical keys aremodified to reflect their new position in the array, but associative keys aren’t affected. Its prototypefollows: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 to the End of an ArrayThe array_push() function adds a value to the end of an array, returning the total count of elements inthe array after the new value has been added. You can push multiple variables onto the arraysimultaneously 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");110

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

Saved successfully!

Ooh no, something went wrong!