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
)
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
- 2 回答
- 0 关注
- 141 浏览
添加回答
举报