gpt4 book ai didi

php - 在 PHP 中,我如何知道哪些值与数组中的特定值相邻?

转载 作者:行者123 更新时间:2023-12-02 06:22:52 27 4
gpt4 key购买 nike

我有一组值。假设数组是这样的:

[apple, banana, coconut, duku, emblica, fig, gooseberry]

假设我知道一个特定值“无花果”。我如何知道它旁边有哪些值,包括之前和之后?

最佳答案

$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] : "";
}

关于php - 在 PHP 中,我如何知道哪些值与数组中的特定值相邻?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347311/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com