note 55558 added to function.array-keys

From: Date: Sun, 07 Aug 2005 09:46:57 +0000
Subject: note 55558 added to function.array-keys
Groups: php.notes 
Request: Send a blank email to php-notes+get-93190@lists.php.net to get a copy of this message
My version of PHP does not support the strict parameter. Moreover, I need a function that could make other comparsion different than equals and stricktly equals. The funcition array_keys_advanced can make the following comparsions: equal, not equal, strictly greater than, equal or greater than, strictly less than, equal or less than. <?php if (!function_exists('array_keys_advanced')) { //{{{ array_keys_advanced /** * Returns an array with the matching keys as values. A comparsion type can * be spcified, even if it should be a strict comparsion or not. * Note: It is not recursive. * * @param array $input * @param string $search_value * @param bool $strict * @param string $comparison: {EQ | NEQ | GT | EGT | LT | ELT} * @return Returns an array with the matching keys as values. * @author alex [@T] d-sn [D@T] com // Alex Galisteo */ function array_keys_advanced() { $nargs = func_num_args(); $arr = array(); $input = null; $search_value = null; $strict = (bool) false; $comparison = "EQ"; $comparsion_types = array("EQ", "NEQ", "GT", "EGT", "LT", "ELT"); switch ($nargs) { case 1: $input = func_get_arg(0); return array_keys($input); break; case 2: $input = func_get_arg(0); $search_value = func_get_arg(1); return array_keys($input, $search_value); break; case 3: $input = func_get_arg(0); $search_value = func_get_arg(1); $strict = (bool) func_get_arg(2); $comparsion = "EQ"; break; case 4: $input = func_get_arg(0); $search_value = func_get_arg(1); $strict = (bool) func_get_arg(2); $comparsion = strtoupper((string) func_get_arg(3)); $comparsion = (in_array($comparsion, $comparsion_types))? $comparsion : "EQ"; break; default: return $arr; break; } foreach ($input as $key => $val) { if ($strict) { if ($comparsion == "EQ" && $search_value === $val) { $arr[] = $key; } elseif ($comparsion == "NEQ" && $search_value !== $val) $arr[] = $key; elseif ($comparsion == "GT" && $search_value > $val) $arr[] = $key; elseif ($comparsion == "EGT" && $search_value >= $val) $arr[] = $key; elseif ($comparsion == "LT" && $search_value < $val) $arr[] = $key; elseif ($comparsion == "ELT" && $search_value <= $val) $arr[] = $key; } else { if ($comparsion == "EQ" && $search_value == $val) $arr[] = $key; elseif ($comparsion == "NEQ" && $search_value != $val) $arr[] = $key; elseif ($comparsion == "GT" && $search_value > $val) $arr[] = $key; elseif ($comparsion == "EGT" && $search_value >= $val) $arr[] = $key; elseif ($comparsion == "LT" && $search_value < $val) $arr[] = $key; elseif ($comparsion == "ELT" && $search_value <= $val) $arr[] = $key; } } return $arr; } //}}} } //endif function_exists ?> ---- Manual Page -- http://www.php.net/manual/en/function.array-keys.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55558 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55558&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55558&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55558&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55558&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55558&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55558&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55558&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93190) next »