我有一系列的价值观.假设数组是这样的:
[apple, banana, coconut, duku, emblica, fig, gooseberry]
Run Code Online (Sandbox Code Playgroud)
让我们说我知道一个具体的价值,"无花果".我如何才能知道它之前和之后的哪个值?
$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)