Is there a better / more elegant / more efficient way of doing this?
I have an array that I'm searching the keys for a value that either matches or is the greatest value that is less than the search value. Hope that makes sense.
My current method is a bit of a brute force attempt that is fine for a small collection of data but this function will need to run many times with a large array.
$needle = '2013-04-04';
$haystack = array (
'2013-01-01' => 1,
'2013-04-03' => 2,
'2013-04-05' => 3,
'2013-07-23' => 4,
'2013-09-12' => 5,
'2013-10-18' => 6,
'2013-11-01' => 7
);
krsort($haystack);
foreach ($haystack as $k => $v)
{
$possibleMatch = $k;
if ($needle >= $k) break;
}
return $possibleMatch
Thanks in advance