在PHP中,如何判断数组中特定值的哪些值相邻?

Jer*_*owe 2 php arrays

我有一系列的价值观.假设数组是这样的:

[apple, banana, coconut, duku, emblica, fig, gooseberry]
Run Code Online (Sandbox Code Playgroud)

让我们说我知道一个具体的价值,"无花果".我如何才能知道它之前和之后的哪个值?

GWW*_*GWW 5

$index = array_search("fig", $array);
$before = "";
$after = "";
if($index === false){
    echo "Not found";
}else{
    $before = $index > 0 ? $array[$index - 1] : "";
    $after = ($index + 1) < count($array) ? $array[$index + 1] : "";
}
Run Code Online (Sandbox Code Playgroud)