Книга: Fedora™ Unleashed, 2008 edition

Arrays

Arrays

Working with arrays is no easy task, but PHP makes it easier by providing a selection of functions that can sort, shuffle, intersect, and filter them. As with other functions, there is only space here to choose a selection; this is by no means a definitive reference to PHP's array functions.

The easiest function to use is array_unique(), which takes an array as its only parameter and returns the same array with all duplicate values removed. Also in the realm of "so easy you do not need a code example" is the shuffle() function, which takes an array as its parameter and randomizes the order of its elements. Note that shuffle() does not return the randomized array; it uses the actual parameter you pass and scrambles it directly. The last too-easy-to-demonstrate function is in_array(), which takes a value as its first parameter and an array as its second and returns true if the value is in the array.

With those out of the way, we can focus on the more interesting functions, two of which are array_keys() and array_values(). They both take an array as their only parameter and return a new array made up of the keys in the array or the values of the array, respectively. The array_values() function is an easy way to create a new array of the same data, just without the keys. This is often used if you have numbered your array keys, deleted several elements, and want to reorder it.

The array_keys() function creates a new array where the values are the keys from the old array, like this:

<?php
 $myarr = array("foo" => "red", "bar" => "blue", "baz" => "green");
 $mykeys = array_keys($myarr);
 foreach($mykeys as $key => $value) {
  echo "$key = $valuen";
 }
?>

That prints "0 = foo", "1 = bar", and "2 = baz".

Several functions are used specifically for array sorting, but only two get much use: asort() and ksort(), the first of which sorts the array by its values and the second of which sorts the array by its keys. Given the array $myarr from the previous example, sorting by the values would produce an array with elements in the order bar/blue, baz/green, and foo/red. Sorting by key would give the elements in the order bar/blue, baz/green, and foo/red. As with the shuffle() function, both asort() and ksort() do their work in place, meaning they return no value, directly altering the parameter you pass in. For interest's sake, you can also use arsort() and krsort() for reverse value sorting and reverse key sorting, respectively.

This code example reverse sorts the array by value and then prints it as before:

<?php
 $myarr = array("foo" => "red", "bar" => "blue", "baz" => "green");
 arsort($myarr);
 foreach($myarr as $key => $value) {
  echo "$key = $valuen";
 }
?>

Previously when discussing constants, we mentioned the extract() function that converts an array into individual variables; now it is time to start using it for real. You need to provide three variables: the array you want to extract, how you want the variables prefixed, and the prefix you want used. Technically, the last two parameters are optional, but practically you should always use them to properly namespace your variables and keep them organized.

The second parameter must be one of the following:

EXTR_OVERWRITE — If the variable exists already, overwrites it.

EXTR_SKIP — If the variable exists already, skips it and moves onto the next variable.

EXTR_PREFIX_SAME — If the variable exists already, uses the prefix specified in the third parameter.

EXTR_PREFIX_ALL — Prefixes all variables with the prefix in the third parameter, regardless of whether it exists already.

EXTR_PREFIX_INVALID — Uses a prefix only if the variable name would be invalid (for example, starting with a number).

EXTR_IF_EXISTS — Extracts only variables that already exist. We have never seen this used.

This next script uses extract() to convert $myarr into individual variables, $arr_foo, $arr_bar, and $arr_baz:

<?php
 $myarr = array("foo" => "red", "bar" => "blue", "baz" => "green");
 extract($myarr, EXTR_PREFIX_ALL, 'arr');
?>

Note that the array keys are "foo", "bar", and "baz" and that the prefix is "arr", but that the final variables will be $arr_foo, $arr_bar, and $arr_baz. PHP inserts an underscore between the prefix and array key.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.071. Запросов К БД/Cache: 0 / 2
поделиться
Вверх Вниз