Title: computation on array-search in php
Tags: php;arrays;search
Question: I have a multi-D array like so:
```   array ( 'JD'=&gt;2457002.50, 67.618536), 
   array ( 'JD'=&gt;2457003.50, 67.619705), 
   array ( 'JD'=&gt;2457004.50, 67.620938)....
```
I have a value say:
```$MyJD = 2457003.9553;
```
I would like to find the value in the array, and if not, match the closest number to the array in question and return the the next index (which i'm assuming is [1])
I was thinking to do an array_search, but it's not going to find the exact number, I want the closest number to $MyValue?

Comment: You could calculate the distance for each number in the array and your value, then perhaps return the index of the smallest (first occurrence) distance.

Here is the accepted answer: This won't return the index but will return the proper array:
```array_multisort(array_map(function($v) use($MyJD) {
                              return abs($v['JD'] - $MyJD);
                          }, $array), $array);
$result = reset($array);
```

Calculate the difference between each ```JD``` value and ```$MyJD```
Sort on the difference (sorting the original) and get the lowest (first) one

Alternately, you could combine using the difference as the key and then sort on the keys:
```$array = array_combine(array_map(function($v) use($MyJD) {
                                     return abs($v['JD'] - $MyJD);
                                 }, $array), $array);
ksort($array);
$result = reset($array);
```
Maybe someone will post a good array_reduce answer.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:06/22/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><arrays><search>', 'Title': 'computation on array-search in php', 'ViewCount': 50, 'id': 68088329, 'user_AboutMe': '<p><strong>Specialties:</strong>\nPHP, Design and System Administration</p>\n', 'user_AccountId': 5187665.0, 'user_DisplayName': 'Danny F', 'user_Reputation': 372.0, 'user_Views': 76.0, 'user_id': 4151581.0}}