An implementation could be something like this:
<?php
$people =
[
'John',
'Adam',
'Terry',
'Gary',
'Wilbur'
];
$get_closest = function ($person) use ($people)
{
$closest = [];
$key = array_search($person, $people);
if($key !== false) {
$before = $key-1;
$after = $key+1;
if(isset($people[$before]))
$closest[] = $people[$before];
if(isset($people[$after]))
$closest[] = $people[$after];
}
return $closest;
};
var_dump($get_closest('Gary'));
Output:
array(2) {
[0]=>
string(5) "Terry"
[1]=>
string(6) "Wilbur"
}
2
solved Implementation of Nearby Person on web page [closed]