比较Php数组中值的位置 $commands=array(); 对于($p=0;$p

比较Php数组中值的位置 $commands=array(); 对于($p=0;$p,php,arrays,position,compare,php-5.3,Php,Arrays,Position,Compare,Php 5.3,我有一个数组$commands。在此数组中,将存储命令列表。我必须检查命令“标记”存储在哪个位置,以及之后是否有某个命令。 一些示例数据可以用$commands表示: “标记”、“忽略”、“拾取”、“随机” 如何操作?您可以使用$index=array\u search(“mark”,$commands)返回第一次出现的命令“mark”的索引,然后您可以使用$commands[$index+1]获取数组中的下一个命令 您还需要检查$index!=null否则它可能会返回$commands数组中的

我有一个数组$commands。在此数组中,将存储命令列表。我必须检查命令“标记”存储在哪个位置,以及之后是否有某个命令。 一些示例数据可以用$commands表示: “标记”、“忽略”、“拾取”、“随机”
如何操作?

您可以使用
$index=array\u search(“mark”,$commands)
返回第一次出现的命令“mark”的索引,然后您可以使用
$commands[$index+1]
获取数组中的下一个命令


您还需要检查
$index!=null
否则它可能会返回
$commands
数组中的第一项,因为
null
被解释为0

刚刚做了一些双重检查,您要首先声明数组包含markfirst的值。array_search将返回一个false,否则,它很容易被转换为0

证明文件:


下面是一组测试用例的演示,以充分表达其工作原理并识别边缘用例:()

*注意,
array\u search()
在未找到指针时返回
false

$commands = array("mark", "ignore", "pick", "random");
//checks if $command contains mark, 
//gets first index as per documentation 
//Or sets index to -1, ie No such value exists.
 $index = in_array("mark",$commands) ? array_search("mark",$commands):-1;
//gets the next command if it exists
 $nextCommand = $index!=-1? $commands[++$index]:"Unable to Find Command: mark";
“或”(
|
)条件将“短路”,因此如果
$index
false
,它将退出条件而不调用第二个表达式(
isset()

输出:

$commands = array("mark", "ignore", "pick", "random");

$attempts = array("mark", "ignore", "pick", "random", "bonk");
foreach($attempts as $attempt){
    echo "$attempt => ";
    $index=array_search($attempt,$commands);
    //                                    vv---increment the value
    if($index===false || !isset($commands[++$index])){  // not found or found last element
        $index=0;                                      // use first element
    }
    echo $commands[$index],"\n";
}

您能提供存储在$commands中的示例数据吗?“mark”、“ignore”、“pick”、“random”如果我的数组看起来像这样,会发生什么$command=array(“mark”、“ignore”、“mark”?它将返回第一个“mark”元素的位置,下一个将是“ignore”,在数组()中执行
,然后
array\u search()
不是很好的编程。为什么不呢?您需要考虑如果mark in不在命令列表中,或者如果
$commands=array(“ignore”、“pick”、“random”)怎么办)
您将返回pick的值,这会使上面的所有答案都不正确。
array\u search()
可以完成所有必要的工作。
in\u array()
是冗余阵列梳理。(不是我的否决票)老实说,我不喜欢在回答中提到
null
,因为检查
false
更准确。我喜欢你的心态。如果你是一个“学习者”,你永远不会是一个“失败者”。自从加入这个社区以来,我已经是一个“学习者”很多次了。这是一个“松散的比较”。当“false/zero-ish”时,这是至关重要的值可能是执行“严格比较”的结果像这样:
==
!==
@mcv这是我将如何做的演示…假设OP希望在指针未找到或指针是最后一个元素时默认为第一个值。我已经测试过了!这个答案实际上是最好的。感谢您的所有输入。但是,如果指针未找到,则我可能会选择标记DNE的值。是的,DNE可能是首选输出。只有OP才能决定什么适合他们的应用。不幸的是,我的系统有点敏感。但我设法解决了这个问题。现在我希望我考虑到了每一个机会。@mickmackusa很抱歉我反应太晚了。我真的很忙。我为了能够使用array_search()管理我的逻辑,我添加了一个switch语句并使其工作:)
$commands = array("mark", "ignore", "pick", "random");

$attempts = array("mark", "ignore", "pick", "random", "bonk");
foreach($attempts as $attempt){
    echo "$attempt => ";
    $index=array_search($attempt,$commands);
    //                                    vv---increment the value
    if($index===false || !isset($commands[++$index])){  // not found or found last element
        $index=0;                                      // use first element
    }
    echo $commands[$index],"\n";
}
mark => ignore
ignore => pick
pick => random
random => mark
bonk => mark