note 114735 added to function.prev
| From: | MicroVBINC | Date: | Mon, 31 Mar 2014 05:09:25 +0000 |
| Subject: | note 114735 added to function.prev | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199079@lists.php.net to get a copy of this message | ||
Some corrections to the previous function.
- When prev() and next() distance is now calculated properly returning the 'closer' one.
- When exact is false, and an item exists with the key searched for, it now returns that entry
instead of next/prev closest
- At ends of the array, sometimes when moving past using next() or prev(), it caused results to die.
This was corrected.
- Added field to result stating whether the match was 'exact'
('exact'=>true), or nearest ('exact'=>false)
<?php
function nearest($array, $value, $exact=false) {
// Initialize Variables
$next = false;
$prev = false;
$return = false;
$arr_begin = false;
$arr_end = false;
$exact_match = false;
if($exact && isset($array[$value])) {
// If exact match found, and searching for exact (not nearest), return result.
$return = Array($value=>$array[$value]);
$exact_match = true;
} elseif (!$exact && isset($array[$value])) {
// If exact match found, and searching for nearest, return result.
$return = Array($value=>$array[$value]);
$exact_match = true;
} else {
if(!isset($array[$value]) && !$exact) {
// Insert element that doesn't exist so nearest can be found
$array[$value] = '-';
}
// Sort array so injected record is near relative numerics
ksort($array); // Sort array on key
// Set start and end keys
$arr_begin = key($array);
end($array);
$arr_end = key($array);
reset($array);
// Loop through array until match is found, then set $prev and $next to the respective keys if
exist
while ( !is_null($key = key($array)) ) {
$val = current($array);
if($key == $value) {
if($value <> $arr_begin) {
prev($array); // Get entry before this one
$prev = key($array);
}
next($array); // Skip current entry as this was what we were looking for nearest to
if($value <> $arr_end) {
next($array); // Get entry after this one
$next = key($array);
}
break;
}
next($array);
}
if($prev && $next) {
if(($value - $prev) < ($next - $value)) {
// Previous is closer
$return = Array($prev=>$array[$prev]);
} else {
// Next is closer
$return = Array($next=>$array[$next]);
}
} elseif ($prev || $next) {
if($prev) {
// Only Previous exists
$return = Array($prev=>$array[$prev]);
} elseif ($next) {
// Only Next exists
$return = Array($next=>$array[$next]);
}
}
}
// Return resulting Array(). $return is false if nothing matches the closest conditions, or if
exact is specified and key does not exist.
if($return) {
return $return+Array('exact'=>$exact_match);
} else {
return false;
}
}
?>
Just to clarify, this function will return either an Array() containing the $key=>$value pair of
the result and 'exact'=>(bool) whether the match was exact or not.
To access the $key=>$value pair, can be done as follows :
<?php
$result = nearest( ... ); // Just here for clarification of the position for the code that
follows.
if($result) {
// Output the Key
echo key($result);
// Output the Value
echo $result( key($result) );
// Output if exact match
if($result['exact']) {
echo 'This was an EXACT match';
} else {
echo 'This result was found with fuzzy logic';
}
} else {
echo 'No result found';
}
?>
----
Server IP: 72.55.140.6
Probable Submitter: 74.14.8.106
----
Manual Page -- http://php.net/manual/en/function.prev.php
Edit -- https://master.php.net/note/edit/114735
Del: integrated -- https://master.php.net/note/delete/114735/integrated
Del: useless -- https://master.php.net/note/delete/114735/useless
Del: bad code -- https://master.php.net/note/delete/114735/bad+code
Del: spam -- https://master.php.net/note/delete/114735/spam
Del: non-english -- https://master.php.net/note/delete/114735/non-english
Del: in docs -- https://master.php.net/note/delete/114735/in+docs
Del: other reasons-- https://master.php.net/note/delete/114735
Reject -- https://master.php.net/note/reject/114735
Search -- https://master.php.net/manage/user-notes.php