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 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃