首页 > 解决方案 > PHP Incrementing Array by 1

问题描述

I have a simple array $iteration=[0,1,2,3,4] and I am trying to build a function to increment it to a max value of $max=12 for example and I can't reuse the same number for 2 keys. But so far I have very little success. Here is what I have now.

//$iteration is my array and $max is the maximum value a key can have.
IncrementIteration($iteration,$max){
    $count=count($iteration);
    while($count > 0){
        if( ($iteration[($count-1)] < $max) ){
            $iteration[($count-1)]++;
            break;
        }
        $count--;
    }
    return $iteration;
}

But this never resets the keys that follows the incremented key and does not take into account if the number has already been used.

Here is what I am looking for as example of results:

print_r(IncrementIteration([0,1,2],12))

Output : Array ( [0] => 0 [1] => 1 [2] => 3 )

print_r(IncrementIteration([0,1,12],12))

Output : Array ( [0] => 1 [1] => 2 [2] => 3 )

print_r(IncrementIteration([0,11,12],12))

Output : Array ( [0] => 1 [1] => 2 [2] => 3 )

This would be the highest possible incrementation.

print_r(IncrementIteration([10,11,12],12))

Output : Array ( [0] => 10 [1] => 11 [2] => 12 )

Thanks for any help on this code.


I am adding the other functions to add more clarity about the purpose of this function.

function ReverseSUM($value,$array){
    global $debug;
    $count=count($array);
    $count=3;
    $values=array();
    while($count > 0){
        //Init of While Iteration
        $iteration=GenerateIteration($count);
        //We iterate
        while(SumIteration($iteration,$array) != $value){
            if($iteration === IncrementIteration($iteration,(count($array)-1))){
                break;
            } else {
                $iteration=IncrementIteration($iteration,(count($array)-1));
            }
            //End of While Iteration
        }
        //End of While Iteration
        if(SumIteration($iteration,$array) == $value){
            array_push($values,$iteration);
        }
        unset($iteration);
        if($debug){echo "</div>";};
        $count--;
    }
    return $values;
}
function GenerateIteration($number){
    $iteration=array();
    $count = 0;
    while($count < $number){
        array_push($iteration,$count);
        $count++;
    }
    return $iteration;
}
function IncrementIteration($iteration,$max){
    $count=count($iteration);
    while($count > 0){
        if( ($iteration[($count-1)] < $max) ){
            $iteration[($count-1)]++;
            break;
        }
        $count--;
    }
    return $iteration;
}
function SumIteration($iteration,$array){
    $result=array();
    foreach($iteration as $key){
        array_push($result,$array[$key]);
    }
    return array_sum($result);
}

标签: phparraysincrement

解决方案


May be some thing like this can help,

  function unique_keys_array($array) {
    $temp_array = array();
    $i = 0;
    $key_array = array();

    foreach($array as $key) {
        if (!in_array($key, $key_array)) {
            $key_array[$i] = $key;
            $temp_array[$i] = $key;
        }
        $i++;
    }
    return $temp_array;
}


print_r(unique_keys_array([1,2,2,3,4,5,6,7,8,8,9,9]));

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

推荐阅读