PHP 将数组递增 1

我有一个简单的数组,我正在尝试构建一个函数以将其递增到例如最大值,并且我无法对2个键重用相同的数字。但到目前为止,我几乎没有取得什么成功。这就是我现在所拥有的。$iteration=[0,1,2,3,4]$max=12


//$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;

}

但这绝不会重置递增的键后面的键,并且不会考虑该数字是否已使用。


以下是我正在寻找的结果示例:


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

输出:数组 ( [0] => 0 [1] => 1 [2] => 3 )


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

输出:数组 ( [0] => 1 [1] => 2 [2] => 3 )


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

输出:数组 ( [0] => 1 [1] => 2 [2] => 3 )


这将是可能的最高增量。


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

输出:数组 ( [0] => 10 [1] => 11 [2] => 12 )


感谢您对此代码的任何帮助。


当年话下
浏览 142回答 2
2回答

牛魔王的故事

也许像这样的事情可以有所帮助,&nbsp; function unique_keys_array($array) {&nbsp; &nbsp; $temp_array = array();&nbsp; &nbsp; $i = 0;&nbsp; &nbsp; $key_array = array();&nbsp; &nbsp; foreach($array as $key) {&nbsp; &nbsp; &nbsp; &nbsp; if (!in_array($key, $key_array)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $key_array[$i] = $key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp_array[$i] = $key;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; }&nbsp; &nbsp; return $temp_array;}print_r(unique_keys_array([1,2,2,3,4,5,6,7,8,8,9,9]));returns Array(&nbsp; &nbsp; [0] => 1&nbsp; &nbsp; [1] => 2&nbsp; &nbsp; [3] => 3&nbsp; &nbsp; [4] => 4&nbsp; &nbsp; [5] => 5&nbsp; &nbsp; [6] => 6&nbsp; &nbsp; [7] => 7&nbsp; &nbsp; [8] => 8&nbsp; &nbsp; [10] => 9)

忽然笑

Here is my final code for my Reverse Sumfunction ReverseSUM($value,$array){&nbsp; &nbsp; ini_set('max_execution_time', 10);&nbsp; &nbsp; if (!function_exists('GenerateIteration')) {&nbsp; &nbsp; &nbsp; &nbsp; function GenerateIteration($number){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global $debug;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $iteration=array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($count < $number){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($iteration,$count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $iteration;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!function_exists('IncrementIteration')) {&nbsp; &nbsp; &nbsp; &nbsp; function IncrementIteration($iteration,$max){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global $debug;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count=count($iteration);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($count > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $iteration[($count-1)] < $max ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $iteration[($count-1)]++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($count != count($iteration)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count2=$count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($count2 <= count($iteration)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($count2 != count($iteration)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if( ($iteration[$count2] < $max) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $iteration[$count2]=($iteration[($count2-1)]+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count2++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $max--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $iteration;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!function_exists('SumIteration')) {&nbsp; &nbsp; &nbsp; &nbsp; function SumIteration($iteration,$array){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global $debug;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result=array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($iteration as $key){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($result,$array[$key]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return array_sum($result);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $count=count($array);&nbsp; &nbsp; $count=3;&nbsp; &nbsp; $values=array();&nbsp; &nbsp; while($count > 0){&nbsp; &nbsp; &nbsp; &nbsp; //Init of While Iteration&nbsp; &nbsp; &nbsp; &nbsp; $iteration=GenerateIteration($count);&nbsp; &nbsp; &nbsp; &nbsp; //We iterate&nbsp; &nbsp; &nbsp; &nbsp; while(SumIteration($iteration,$array) != $value){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($iteration === IncrementIteration($iteration,(count($array)-1))){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $iteration=IncrementIteration($iteration,(count($array)-1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //End of While Iteration&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //End of While Iteration&nbsp; &nbsp; &nbsp; &nbsp; if(SumIteration($iteration,$array) == $value){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($values,$iteration);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; unset($iteration);&nbsp; &nbsp; &nbsp; &nbsp; $count--;&nbsp; &nbsp; }&nbsp; &nbsp; return $values;}And here is how I display the results:<?php foreach($recap as $line => $value){ ?>&nbsp; &nbsp; <?php if($line<2){?>&nbsp; &nbsp; <table border="1">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th colspan="2" style="text-align:left;">Line <?=$line?> - <?=$value?></th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Iteration</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Values</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <?php foreach(ReverseSUM($value,$invoice) as $iteration => $values){?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?=$iteration?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php foreach($values as $array){?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?=($array +1)?><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; </table>&nbsp; &nbsp; <?php } ?><?php } ?>The $recap array simply contains the total values we are searching for. And the $invoice array contains all the invoice lines totals.Code available on GitHub : https://github.com/LouisOuellet/ReverseSUMSheers
打开App,查看更多内容
随时随地看视频慕课网APP