From 3c70493ae1a5fe38276f5ef38e0076e529d6703d Mon Sep 17 00:00:00 2001 From: Miles Lott Date: Fri, 28 Dec 2001 07:07:32 +0000 Subject: [PATCH] Add array_pop(); test and confirm array_pop/array_shift; add comments to each function; order functions alphabetically --- phpgwapi/inc/php3_support_functions.inc.php | 177 +++++++++++++++----- 1 file changed, 136 insertions(+), 41 deletions(-) diff --git a/phpgwapi/inc/php3_support_functions.inc.php b/phpgwapi/inc/php3_support_functions.inc.php index 8f203d2c88..18221c8dfa 100755 --- a/phpgwapi/inc/php3_support_functions.inc.php +++ b/phpgwapi/inc/php3_support_functions.inc.php @@ -24,27 +24,10 @@ /* $Id$ */ - function is_bool($var) - { - $retval = gettype($var) ; - if ( strcmp( $retval, 'unknown type') == 0 ) - { - /* Chances are that we have a boolean */ - if ($var == True || $var == False) - { - return True; - } - else - { - return False; - } - } - else - { - return False ; - } - } - + /* array_keys (PHP 4 >= 4.0.0) + * array array_keys (array input, mixed [search_value]) + * array_keys() returns the keys, numeric and string, from the input array. + */ function array_keys ($arr, $term='') { $t = array(); @@ -59,7 +42,58 @@ } } -/* + /* array_pop (PHP 4 >= 4.0.0) + * mixed array_pop (array array) + * array_pop() pops and returns the last value of the array, shortening the array by + * one element. If array is empty (or is not an array), NULL will be returned. + */ + function array_pop(&$array) + { + if(!is_array($array) || @count($array) == 0) + { + return NULL; + } + + reset($array); + $rtrn = array(); + + $i = count($array) + 1; + + while(list($key,$value) = each($array)) + { + $i--; + if($i == 1) + { + $last = $value; + } + else + { + $rtrn[$key] = $value; + } + } + $array = $rtrn; + return $last; + } + + /* array_push + * int array_push (array array, mixed var [, mixed ...]) + * array_push() treats array as a stack, and pushes the passed variables onto the end + * of array. The length of array increases by the number of variables pushed. Has the + * same effect as '$array[] = $var;' repeated for each var. + */ + /* + function array_push() + { + } + */ + + /* array_reverse (PHP 4 >= 4.0.0) + * array array_reverse (array array [, bool preserve_keys]) + * array_reverse() takes input array and returns a new array with the order of the + * elements reversed, preserving the keys if preserve_keys is TRUE. + * Note: The second parameter was added in PHP 4.0.3. + */ + /* function array_reverse ($array, $preserve_keys = False) { for(list($key,$value) = @end($array); list($key,$value) = @prev($array); ) @@ -68,8 +102,13 @@ } return $temp_array; } -*/ - + */ + + /* array_search (PHP 4 >= 4.0.5) + * mixed array_search (mixed needle, array haystack [, bool strict]) + * Searches haystack for needle and returns the key if it is found in the array, FALSE + * otherwise. + */ function array_search ($needle, $haystack, $strict = False) { @reset($haystack); @@ -83,26 +122,17 @@ return False; } - function in_array ($needle, $haystack, $strict = False) - { - if(is_array ($haystack) && count($haystack)) - { - for(@reset($haystack); $x=each($haystack); ) - { - if($needle==$x[1] && (!$strict || gettype($needle)==gettype($x[1]))) - { - return True; - } - } - return False; - } - } - + /* array_shift (PHP 4 >= 4.0.0) + * mixed array_shift (array array) + * array_shift() shifts the first value of the array off and returns it, shortening the + * array by one element and moving everything down. If array is empty (or is not an + * array), NULL will be returned. + */ function array_shift(&$array) { - if(!is_array($array)) + if(!is_array($array) || @count($array) == 0) { - return ''; + return NULL; } reset($array); @@ -125,6 +155,10 @@ return $one; } + /* array_unique (PHP 4) + * array array_unique (array array) + * array_unique() takes input array and returns a new array without duplicate values. + */ function array_unique ($array) { reset($array); @@ -145,6 +179,67 @@ return $new_array; } + /* array_unshift (PHP 4 >= 4.0.0) + * int array_unshift (array array, mixed var, mixed [...]) + * array_unshift() prepends passed elements to the front of the array. Note that the + * list of elements is prepended as a whole, so that the prepended elements stay in the + * same order. Returns the new number of elements in the array. + */ + /* + function array_unshift() + { + } + */ + + /* in_array (PHP 4 >= 4.0.0) + * bool in_array (mixed needle, array haystack [, bool strict]) + * Searches haystack for needle and returns TRUE if it is found in the array, FALSE + * otherwise. + */ + function in_array ($needle, $haystack, $strict = False) + { + if(is_array ($haystack) && count($haystack)) + { + for(@reset($haystack); $x=each($haystack); ) + { + if($needle==$x[1] && (!$strict || gettype($needle)==gettype($x[1]))) + { + return True; + } + } + return False; + } + } + + /* is_bool (PHP 4 >= 4.0.0) + * bool is_bool (mixed var) + * is_bool -- Finds out whether a variable is a boolean + */ + function is_bool($var) + { + $retval = gettype($var) ; + if ( strcmp( $retval, 'unknown type') == 0 ) + { + /* Chances are that we have a boolean */ + if ($var == True || $var == False) + { + return True; + } + else + { + return False; + } + } + else + { + return False ; + } + } + + /* str_repeat (PHP 4 >= 4.0.0) + * string str_repeat (string input, int multiplier) + * Returns input_str repeated multiplier times. multiplier has to be greater than 0. + */ function str_repeat($input,$multiplier) { for($i=0,$output='';$i<$multiplier;$i++)