<<

. 31
( 132 .)



>>


[0] => mourning, [2] => carter, [3] => davis, [4] => miller

Additional arrays can be added to the function. For example:

$knicks = array(“sprewell”, “houston”, “ewing”, “childs”);
$all_stars = array(“mourning”, “houston”, “carter”, “davis”,
“miller”);
$non_knick_allstars = array_diff($all_stars, $knicks,
array(“carter”));

Given this, carter will also be removed from the returned array.

ARRAY_INTERSECT() This function returns the array elements that two (or more)
arrays have in common.

array array_intersect (array array1, array array2 [, array ...])

IN_ARRAY() This very convenient function will search all the values of an array
and return TRUE if the value in the first argument is found in the array in the sec-
ond argument.

bool in_array (mixed needle, array haystack)

ARRAY_KEY_EXISTS()

bool array_key_exists ( mixed key, array search )
160 Part II: Working with PHP

This function returns TRUE if the key value key exists in the array search.
The difference between this function and isset($array[˜key™]) is that if
$array[˜key™] has a value of NULL, isset($array[˜key™]) returns FALSE, while
array_key_exists() returns TRUE.

ARRAY_POP() The array_pop() function returns the last element in an array, and
removes that element from the original array.

mixed array_pop (array array)

For example:

$array = array(1,2,3,4,5);
$int = array_pop($array);

After this runs, $array will contain (1,2,3,4) and $int will contain 5.

ARRAY_PUSH() This function adds elements to the end of the array indicated in
the first argument.

array_push (array array, mixed var [, mixed ...])

The additional arguments will be values you wish to tack onto the array:

$array = array (1,2,3);
array_push($array,4,5,6);

The resulting array will contain 1,2,3,4,5,6.

ARRAY_SHIFT()

mixed array_shift (array array)

This function works identically to array_pop(), except that it takes the first ele-
ment instead of the last.

ARRAY_UNSHIFT()

array_unshift(array array, mixed var [, mixed ...])

This function works identically to array_push(), except that it adds new ele-
ments to the beginning instead of the end of the array.
Chapter 6: PHP™s Built-in Functions 161

ARRAY_VALUES()

array array_values(array input)

Given an input array, array_values() returns all the values from the array.
Note that it does not return key names; instead, it outputs numbered values. The
following is an example output:

Array
(
[0] => Jay
[1] => 5.4
[2] => blue
)

ARRAY_WALK()

array array_walk (array array, callback function, [mixed data])

This function takes an array and a function as arguments and passes each ele-
ment of the array through the function. It returns an array containing all trans-
formed elements, in the original order.
So if you have defined a function altupper, which modifies the case of strings
in an annoying way, then, given

$alternating_uppercase = array_walk(array(˜blue™, ˜red™, ˜pink™,
˜yellow™), “altupper”);

$alternating_uppercase will contain the following:

[0] => bLuE
[1] => rEd
[2] => pInK
[3] => yElLoW

Note that the called function must not modify the array itself.

EACH()

array each (array array)

This function returns the key/value pair at the current location of the array cur-
sor (the logical pointer that indicates which element of the array is being examined)
and advances the cursor one element.
162 Part II: Working with PHP

RESET()

mixed reset (array array)

This function resets the cursor of an array to the beginning, and returns the
value of the array™s first element.

ARRAY FUNCTIONS NOT USED IN THIS BOOK
Again, PHP contains many great array functions. Here are some of the highlights
(from our point of view, anyway).

ARRAY_COUNT_VALUES() This nifty function will return an associative array, the
keys of which will be all of the unique values within the array.

array array_count_values (array input)

The values of the resulting array will be an integer representing the number of
times the value appears within the array:

$array = array(“yes”,”no”,”no”,”yes”,”why”);
$result = array_count_values($array);

After this $result will contain:

[yes] =>, 2, [no] => 2, [why] => 1



ARRAY_RAND() This function will pick one or more random elements from an
array.

mixed array_rand (array input [, int num_req])

Note that array_rand() does not pick the value; rather, it picks the key of the
chosen elements. For example:

srand ((double) microtime() * 1000000);
$names = array(“jay”, “brad”, “john”, “Jeff”);
$rand_keys = array_rand ($names, 2);

In this example, $rand_keys will contain an array with two numbers. To get the
values from the $names array, you will first need to get to the key value extracted
by array_rand(), and so you will need to use something like this:

echo $names[$rand_keys[0]];
Chapter 6: PHP™s Built-in Functions 163


Seed the random number generator only once per script. You might even
want to think about using a shared variable so that you can seed the gener-
ator only once, period, to deal with a problem that™s beginning to appear
more frequently as processors get faster. If processor speed continues to
increase and transaction volume increases, there is a ceiling to reach at
which point the random numbers stop being random because your micro
time() value is the same in two consecutive calls.



SHUFFLE() This function randomizes the elements in an array.

void shuffle (array array)

You will need to seed the random number generator before using it. For
instance:

srand ((double) microtime() * 1000000)
shuffle ($array);

SORT() If no second argument is given, this function will sort an array in ascend-
ing or alphabetical order.

void sort (array array [, int sort_flags])

The flags can be of two kinds:

— SORT_NUMERIC ” Compare items numerically

— SORT_STRING ” Compare items as strings

If the array you wish to sort contains only numbers, PHP will sort it numerically;
if the array contains only strings, it will be sorted alphabetically. If the array con-
tains both strings and numbers, it defaults to sorting by a string.


PHP offers many other ways to sort arrays. Please look at the manual entries
for arsort(), ksort(), rsort(), and usort().




Object/class functions
PHP has a number of functions concerned with classes (which are templates from
which objects are created) and objects themselves. The examples in this book use a
164 Part II: Working with PHP

few functions that retrieve information about the defined objects, classes, and
methods. Here they are.

is_a()
Use this function to determine whether a given object is an instance of a given class
or an instance of a class descended from the given class. Specify the object in the
first argument and the class name in the second; the function will return TRUE if the
one is an instance of the other.

bool is_a (object object, string class_name)

is_object()
This function, much like the other is_ functions, returns TRUE if the argument is an
object, and FALSE otherwise.

bool is_object (mixed var)

get_class()
Given an object, this function returns the name of the class of which the object is
an instance.

string get_class (object obj)

get_object_vars()
Given an object, this function returns an associative array containing the object™s
current properties.

array get_object_vars (object obj)

method_exists()
Given an object and a method name, this function returns TRUE if the method
(function) is defined for the object.

bool method_exists (object object, string method_name)


Print functions
Several functions enable you to print information to the screen. Only two pop up in
this book, but you should be aware of all the functions listed in this section.

PRINT FUNCTIONS USED IN THIS BOOK
In this case the word “functions” may be something of a misnomer. For instance,
print() is probably better described as a language construct. (The useful difference
Chapter 6: PHP™s Built-in Functions 165

is that you are not required to use parentheses around the arguments.) In any case,
you will use all of these very much as you will use functions; thus, they are included
here.

PRINT() As you would expect, this prints what you specify.

void print ( string str )

ECHO() This also isn™t a function, but a language construct. We use it constantly
throughout this book, so at this point you probably know what it does.

<<

. 31
( 132 .)



>>