Joel Has Simple Questions Joel Has Simple Questions - 21 days ago 5
PHP Question

PHP Working with an array to find ranges and missing numbers

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.

Answer

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