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 your parameter as a reference 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:

$myarr = array("foo" => "red", "bar" => "blue", "baz" => "green");

$mykeys = array_keys($myarr); foreach($mykeys as $key => $value) { echo "$key = $value\n";

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:

$myarr = array("foo" => "red", "bar" => "blue", "baz" => "green"); arsort($myarr);

foreach($myarr as $key => $value) { echo "$key = $value\n";

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 on to 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.

You can also, optionally, use the bitwise or operator, |, to add in Extr_REFs to have exTRact() use references for the extracted variables. In general use, Extr_PREFix_ALL is preferred because it guarantees namespacing. extr_refs is required only if you need to be able to change the variables and have those changes reflected in the array.

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

$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.

Was this article helpful?

0 0

Post a comment