duanaozhong0696 2016-11-16 16:29
浏览 26
已采纳

PHP使用数组查找范围和缺少的数字

I have an array that looks like this:

    [0] => Array
    (
        [1] => 5
        [2] => 4
        [3] => 3
        [5] => 1
        [7] => 1
        [8] => 2
        [9] => 3
        [10] => 4
        [11] => 5
    )

[1] => Array
    (
        [1] => 6
        [2] => 5
        [4] => 3
        [5] => 2
        [6] => 1
        [8] => 3
        [9] => 4
        [10] => 5
        [11] => 6
    )

[2] => Array
    (
        [1] => 7
        [2] => 6
        [3] => 5
        [4] => 4
        [5] => 3
        [6] => 2
        [7] => 3
        [8] => 4
        [11] => 7
    )

I have an order of operations I'm trying to go through and I really don't know where to go from here. Any suggestions would be of great help.

  1. First I give my class the number of items I want to return. For example here we'll use 4.

  2. I want to loop through and find the item in the array that has the lowest value.

  3. I want to look at the keys to the items around (it included) and be sure they're not missing a number if they are..reject it..

In this example the first one you would come to would be:

    [5] => 1

Now looking around it you see that the keys are missing some numbers. So no combination of 4 will get that to match any 4 in the proper order.

    [1] => 5
    [2] => 4
    [3] => 3
    [5] => 1  //this one
    [7] => 1
    [8] => 2
    [9] => 3

In this situation I want it to move onto the next case.

    [7] => 1

Notice this one will work due to the keys being 7,8,9,10.

    [7] => 1
    [8] => 2
    [9] => 3
    [10] => 4

This is what I would like returned first..but I don't even know how to begin to get there.

Further more there are situations like this..Say for example there are no 1's at all in the dataset..and only this lone 2 in the last one.

    [0] => Array
    (
        [1] => 5
        [2] => 4
        [3] => 3
        [5] => 3
        [7] => 3
        [8] => 3
        [9] => 3
        [10] => 4
        [11] => 5
    )

[1] => Array
    (
        [1] => 6
        [2] => 5
        [4] => 3
        [5] => 3
        [6] => 3
        [8] => 3
        [9] => 4
        [10] => 5
        [11] => 6
    )

[2] => Array
    (
        [1] => 7
        [2] => 6
        [5] => 3
        [6] => 2 // this one
        [7] => 3
        [8] => 4
        [11] => 7
    )

The following wont work:

    [6] => 2 // this one
    [7] => 3
    [8] => 4
    [11] => 7

but this one will:

        [5] => 3
        [6] => 2 // this one
        [7] => 3
        [8] => 4

I have no idea on how to approach this.. If someone could offer some advice it would be GREATLY appreciated. Thanks so much in advance.

  • 写回答

2条回答 默认 最新

  • douyu9159 2016-11-16 16:54
    关注

    The following assumes your data is in an array called $data. I'll describe it in steps, then pull it all together as a function.

    Step 1 find the min value:

    $minValue=min($data);
    

    Step 2 loop through the array looking for all values that are that value:

    foreach($data as $index => $value){
    
        if($value == $minValue){
             // $index is a candidate!
        }
    }
    

    Step 3 Check if $valuesToReturn entries exist after index:

        $success=true;
    
        for($i=1;$i<=$valuesToReturn;$i++){
    
            if(!array_key_exists($index + $i,$data)){
    
                // Candidate failed.
                $success=false;
                break;
    
            }
    
        }
    

    Step 4 If the candidate was successful, return it.

        if($success){
            return $index;
        }
    

    Putting that all together, we get this:

    function findSuitableIndex($data,$valuesToReturn){
    
        // Min:
        $minValue=min($data);
    
        foreach($data as $index => $value){
    
            if($value == $minValue){
                // $index is a candidate!
    
                // test if index is actually suitable:
                $success=true;
    
                for($i=1;$i<=$valuesToReturn;$i++){
    
                   if(!array_key_exists($index + $i,$data)){
    
                        // Candidate failed.
                        $success=false;
                        break;
    
                    }
    
                }
    
                if($success){
                    return $index;
                }
    
            }
    
        }
    
        // If we fell down here, we failed to find any successful results.
        return -1;
    }
    

    Working sample:

    Code on eval.in

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。