返回键,如果3个相邻值相匹配 [英] Returning keys if 3 adjacent values match

查看:108
本文介绍了返回键,如果3个相邻值相匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下阵列

[true, false, false, true, true, false, false, true, true, true, false, false]

我知道如何获得中点并使用 CEIL()地板()来得到最接近的整数值以及如何检查之前和这个中点后的值是否具有相同的值作为我的中点。

I know how to get midpoint and use either ceil() or floor() to get the nearest integer value and how to check whether the value before and after this midpoint have the same value as my midpoint.

我的问题是,我需要三个真正值彼此相邻,为我的病情恢复真正。在这个例子中,采用中点和邻接它的值将返回。中点是我的prefered这里位置我需要做的事情还有,如果我的条件返回​​真正。当这个然而返回,我需要在这里有三个真正彼此相邻值之任何其他职位。我需要拿到钥匙,因为我需要的关键,确定本中点,因为我需要插入值存在。

My problem is, I need three true values next to each other for my condition to return true. In this example, using midpoint and the values adjacent to it will return false. Midpoint is my prefered position here as I need to do something there if my condition returns true. When this however returns false, I need to get any other position where there are three true values next to each other. I need to get the keys as I need the key to detemine the midpoint as I need to insert a value there.

因此​​,从我的例子,我需要键被返回号7,8和9中,这些键的不值

So from my example, I need key number 7, 8 and 9 to be returned, not the values of these keys

任何建议,以实现这一目标?

Any suggestions to achieve this?

推荐答案

林不知道ü想这个还是不行。反正PLZ试试下面code结果

Im not sure u want this or not. Anyway plz try the below code

$a = array(true, false, false, true, true, false, false, true, true, true, false, false);
for ($i=0;$i<(sizeof($a)-1);$i++)
{
    if($i!=0&&$i!=(sizeof($a)-1))
    {
        if($a[$i-1]==$a[$i])
        {
            if($a[$i]==$a[$i+1])
            {
                echo ($i-1).','.$i.','.($i+1).'<br>';
            }
        }

    }
}

这篇关于返回键,如果3个相邻值相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆