11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

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

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

www.it-ebooks.infoCHAPTER 5 • ARRAYSDetermining Unique Array ValuesThe array_unique() function removes all duplicate values found in an array, returning an arrayconsisting of solely unique values. Its prototype follows:array array_unique(array array [, int sort_flags = SORT_STRING])An example follows:$states = array("Ohio", "Iowa", "Arizona", "Iowa", "Ohio");$uniqueStates = array_unique($states);print_r($uniqueStates);This returns the following:Array ( [0] => Ohio [1] => Iowa [2] => Arizona )The optional sort_flags parameter (added in <strong>PHP</strong> 5.2.9) determines how the array values aresorted. By default, they will be sorted as strings; however, you also have the option of sorting themnumerically (SORT_NUMERIC), using <strong>PHP</strong>’s default sorting methodology (SORT_REGULAR), or according to alocale (SORT_LOCALE_STRING).Sorting ArraysTo be sure, data sorting is a central topic of computer science. Anybody who’s taken an entry-levelprogramming class is well aware of sorting algorithms such as bubble, heap, shell, and quick. Thissubject rears its head so often during daily programming tasks that the process of sorting data is ascommon as creating an if conditional or a while loop. <strong>PHP</strong> facilitates the process by offering amultitude of useful functions capable of sorting arrays in a variety of manners.■ Tip By default, <strong>PHP</strong>’s sorting functions sort in accordance with the rules as specified by the English language. Ifyou need to sort in another language, say French or German, you’ll need to modify this default behavior by settingyour locale using the setlocale() function.Reversing Array Element OrderThe array_reverse() function reverses an array’s element order. Its prototype follows:array array_reverse(array array [, boolean preserve_keys])If the optional preserve_keys parameter is set to TRUE, the key mappings are maintained.Otherwise, each newly rearranged value will assume the key of the value previously presiding at thatposition:119

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

Saved successfully!

Ooh no, something went wrong!