note 114733 added to function.prev

From: Date: Mon, 31 Mar 2014 01:17:56 +0000
Subject: note 114733 added to function.prev
Groups: php.notes 
Request: Send a blank email to php-notes+get-199077@lists.php.net to get a copy of this message
This function searches for the closest element in an array by key value, and returns the key/value pair, or false if not found. <?php function nearest($array, $value, $exact=false) { // Initialize Variables $next = false; $prev = false; $return = false; if(!isset($array[$value]) && !$exact) { // Insert element that doesn't exist so nearest can be found $array[$value] = '-'; } if($exact && isset($array[$value])) { // If exact match found, and searching for exact (not nearest), return result. $return = Array($value=>$array[$value]); } else { // Sort array so injected record is near relative numerics ksort($array); // Sort array on key // 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) { 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 next($array); // Get entry after this one $next = key($array); break; } next($array); } if($prev && $next) { if(($long - $prev) > ($next - $long)) { // 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. return $return; } ?> Example usage (to lookup the closest color in an array) <?php $mycolors= Array( 5001046=>'Abbey', 1774596=>'Acadia', 8171681=>'Acapulco', 6970651=>'Acorn', 13238245=>'Aero Blue', 7423635=>'Affair', 8803850=>'Afghan Tan', 13943976=>'Akaroa', 16777215=>'Alabaster', 16116179=>'Albescent White', 10176259=>'Alert Tan', 30371=>'Allports' ); // Color to search for in Hex $color = 'C0C0C0'; // Convert Hex to Long to compare with array() keys $colorlong = base_convert($color,16,10); // Check for an exact match $result = nearest($mycolors, $colorlong, true); if($result) { echo "An exact match was found for #" . $color . " which is called '" . $result[key($result)] . "'"; } else { echo "No exact match was found"; } if(!$result) { // Check for closest match $result = nearest($mycolors, $colorlong, true); if($result) { // Match found echo "The closest match for #" . $color . " is #" . base_convert(key($result),10,16) . " which is called '" . $result[key($result)] . "'"; } else { // No match found echo "No match was found for #" . $color; } } ?> ---- Server IP: 64.68.200.35 Probable Submitter: 74.14.8.106 ---- Manual Page -- http://php.net/manual/en/function.prev.php Edit -- https://master.php.net/note/edit/114733 Del: integrated -- https://master.php.net/note/delete/114733/integrated Del: useless -- https://master.php.net/note/delete/114733/useless Del: bad code -- https://master.php.net/note/delete/114733/bad+code Del: spam -- https://master.php.net/note/delete/114733/spam Del: non-english -- https://master.php.net/note/delete/114733/non-english Del: in docs -- https://master.php.net/note/delete/114733/in+docs Del: other reasons-- https://master.php.net/note/delete/114733 Reject -- https://master.php.net/note/reject/114733 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199077) next »