poorly written, please optimize
An array of user IDs
$uids = [1,2,3,5,6,8,9,11,13,25,65];
Each key value of this array represents a UID
Amount array
$amounts = [ 1=>12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13=>12000, 25=>22000, 65=>123123];
The key name of this array corresponds to the key value of the $uid array.
Loop $uids This array, take out the corresponding amount $amount,
If this amount is greater than or equal to 12000 ($boundary), the total amount ($totals) plus this $amount
If this amount is less than 12000, then cycle down again, loop to these several $amount add greater than or equal to 12000, and then in the total amount ($totals) plus these several $amount and
Take up to three layers.
Finally, the value of $totals is obtained.
My current code:
12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13 =>12000, 25=>22000, 65=>123123]; $totals = 0;foreach ($uids as $k = = $uid) {$amont = $amounts [$uid]; if ($amont >= $boundary) {$totals + = $amont; }else{$next = Get_next ($uids, $k +1, $amont); if ($next && Is_array ($next)) {$curKey = $next [0];//amouts Index 3 $totals + = $next [2]; Then get one layer down $nextKey = $curKey +1; if (!isset ($uids [$nextKey])) {break; } $nextUid = $uids [$nextKey]; $nextAmount = $amounts [$nextUid]; if ($nextAmount >= $boundary) {$totals + = $nextAmount; }else{$last = Get_next ($uids, $nextKey +1, $nextAmount); if ($last && Is_array ($last)) {$totals + = $last [2]; }}} break; //jump out of the main loop}}echo $totals; exit;function get_next ($uids, $start, $prevAmount) {global $amounts, $boundary; $leaves = Array_slice ($uids, $start, Count ($uids), true); if ($leaves) {foreach ($leaves as $k = = $uid) {$amount = $prevAmount + $amounts [$uid]; if ($amount >= $boundary) {return [$k, $uid, $amount]; Break }else{return Get_next ($uids, $k +1, $amount); }}} return 0;}
Draw $totals=47500
Reply content:
Poorly written, please optimize
An array of user IDs
$uids = [1,2,3,5,6,8,9,11,13,25,65];
Each key value of this array represents a UID
Amount array
$amounts = [ 1=>12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13=>12000, 25=>22000, 65=>123123];
The key name of this array corresponds to the key value of the $uid array.
Loop $uids This array, take out the corresponding amount $amount,
If this amount is greater than or equal to 12000 ($boundary), the total amount ($totals) plus this $amount
If this amount is less than 12000, then cycle down again, loop to these several $amount add greater than or equal to 12000, and then in the total amount ($totals) plus these several $amount and
Take up to three layers.
Finally, the value of $totals is obtained.
My current code:
12000, 2=>500, 3=>11000, 5=>1000, 6=>11000, 8=>12000, 9=>12000, 11=>11000, 13 =>12000, 25=>22000, 65=>123123]; $totals = 0;foreach ($uids as $k = = $uid) {$amont = $amounts [$uid]; if ($amont >= $boundary) {$totals + = $amont; }else{$next = Get_next ($uids, $k +1, $amont); if ($next && Is_array ($next)) {$curKey = $next [0];//amouts Index 3 $totals + = $next [2]; Then get one layer down $nextKey = $curKey +1; if (!isset ($uids [$nextKey])) {break; } $nextUid = $uids [$nextKey]; $nextAmount = $amounts [$nextUid]; if ($nextAmount >= $boundary) {$totals + = $nextAmount; }else{$last = Get_next ($uids, $nextKey +1, $nextAmount); if ($last && Is_array ($last)) {$totals + = $last [2]; }}} break; //jump out of the main loop}}echo $totals; exit;function get_next ($uids, $start, $prevAmount) {global $amounts, $boundary; $leaves = Array_slice ($uids, $start, Count ($uids), true); if ($leaves) {foreach ($leaves as $k = = $uid) {$amount = $prevAmount + $amounts [$uid]; if ($amount >= $boundary) {return [$k, $uid, $amount]; Break }else{return Get_next ($uids, $k +1, $amount); }}} return 0;}
Draw $totals=47500