php - 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);
}
解决方案
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
)
推荐阅读
- discord.py - 当用户加入特定语音频道时,如何制作一个在特定频道中发送嵌入消息的 discord.py 机器人
- android - Android 安装其他应用程序的权限
- visual-studio-code - 在 VScode 中使用 GDB 将 watch 变量的格式设置为十六进制
- c++ - C ++中网络之间的套接字
- spring - 在春季批处理写入器中写入多个不相关的表
- android - Flutter 社交分享插件(facebook / instagram)不起作用
- ruby-on-rails - 使用未确认的帐户登录时,覆盖设计会话控制器以触发“确认您的电子邮件”电子邮件
- c - 如何在不影响性能的情况下实现原始类型的类型安全?
- reactjs - 制作 Service Worker 后,我的网站仅在我删除缓存时加载
- android - 如何在android的firebase recyclerview中播放和暂停音频