亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 將數組遞增 1

PHP 將數組遞增 1

PHP
當年話下 2022-09-03 16:02:33
我有一個簡單的數組,我正在嘗試構建一個函數以將其遞增到例如最大值,并且我無法對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 )感謝您對此代碼的任何幫助。
查看完整描述

2 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

也許像這樣的事情可以有所幫助,


  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

)


查看完整回答
反對 回復 2022-09-03
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

Here is my final code for my Reverse Sum


function ReverseSUM($value,$array){

    ini_set('max_execution_time', 10);

    if (!function_exists('GenerateIteration')) {

        function GenerateIteration($number){

            global $debug;

            $iteration=array();

            $count = 0;

            while($count < $number){

                $count++;

                array_push($iteration,$count);

            }

            return $iteration;

        }

    }

    if (!function_exists('IncrementIteration')) {

        function IncrementIteration($iteration,$max){

            global $debug;

            $count=count($iteration);

            while($count > 0){

                if( $iteration[($count-1)] < $max ){

                    $iteration[($count-1)]++;

                    if($count != count($iteration)){

                        $count2=$count;

                        while($count2 <= count($iteration)){

                            if($count2 != count($iteration)){

                                // if( ($iteration[$count2] < $max) ){

                                    $iteration[$count2]=($iteration[($count2-1)]+1);

                                // }

                            }

                            $count2++;

                        }

                    }

                    break;

                }

                $max--;

                $count--;

            }

            return $iteration;

        }

    }

    if (!function_exists('SumIteration')) {

        function SumIteration($iteration,$array){

            global $debug;

            $result=array();

            foreach($iteration as $key){

                array_push($result,$array[$key]);

            }

            return array_sum($result);

        }

    }

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

        $count--;

    }

    return $values;

}

And here is how I display the results:


<?php foreach($recap as $line => $value){ ?>

    <?php if($line<2){?>

    <table border="1">

        <tr>

            <th colspan="2" style="text-align:left;">Line <?=$line?> - <?=$value?></th>

        </tr>

        <tr>

            <th>Iteration</th>

            <th>Values</th>

        </tr>

        <?php foreach(ReverseSUM($value,$invoice) as $iteration => $values){?>

            <tr>

                <td><?=$iteration?></td>

                <td>

                    <?php foreach($values as $array){?>

                        <?=($array +1)?><br />

                    <?php } ?>

                </td>

            </tr>

        <?php } ?>

    </table>

    <?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/ReverseSUM


Sheers


查看完整回答
反對 回復 2022-09-03
  • 2 回答
  • 0 關注
  • 163 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號