php-如何计算数组值和键,直到达到目标
我有一个数组的值,我需要计算它们,但只有在我达到$target金额。我需要知道需要多少数组键才能到达目标($count)以及相应值的总和($total)。以下是我正在使用的阵列:php-如何计算数组值和键,直到达到目标,php,arrays,Php,Arrays,我有一个数组的值,我需要计算它们,但只有在我达到$target金额。我需要知道需要多少数组键才能到达目标($count)以及相应值的总和($total)。以下是我正在使用的阵列: $numbers = Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 6 [5] => 1 [6] => 5.5 [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] =
$numbers = Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 6 [5] => 1 [6] => 5.5 [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 11 )
使用$target=9
时,$total
应该是10,$count应该是5,但我得到的是$total=9
和$count=9
,因为代码似乎是在计数键而不是值。同样,如果目标是12,那么$total
应该是16.5,$count
应该是7,但我得到12和12
希望一切都有意义。如果有人能编辑此代码,使其适用于任何数字数组和任何目标,将不胜感激
$count=0;
$target=9;
$total=0;
foreach($numbers as $key => $value){
while($total < $target) {
$total = $total+$value;
$count++;
}
}
echo "total is $total and count is $count";
$count=0;
$target=9;
$total=0;
foreach($key=>$value的数字){
而($total<$target){
$total=$total+$value;
$count++;
}
}
echo“总计为$total,计数为$count”;
添加if语句
$total = 0;
foreach($numbers as $key => $value)
{
$total = $total+$value;
if($total >= $target)
{
$count = $key+1;
break;
}
}
您不需要while
循环。添加if语句
$total = 0;
foreach($numbers as $key => $value)
{
$total = $total+$value;
if($total >= $target)
{
$count = $key+1;
break;
}
}
您不需要
while
循环。将$outgoing重命名为$target,并将while更改为if
$count=0;
$target=9;
$total=0;
foreach($numbers as $key => $value){
if($total < $target) {
$total = $total+$value;
$count++;
}
else
{
break;
}
}
echo "total is $total and count is $count";
$count=0;
$target=9;
$total=0;
foreach($key=>$value的数字){
如果($total<$target){
$total=$total+$value;
$count++;
}
其他的
{
打破
}
}
echo“总计为$total,计数为$count”;
UPD:重写代码以避免使用带中断的循环条目将$outgoing重命名为$target,并将while更改为if
$count=0;
$target=9;
$total=0;
foreach($numbers as $key => $value){
if($total < $target) {
$total = $total+$value;
$count++;
}
else
{
break;
}
}
echo "total is $total and count is $count";
$target = 9;
$total = 0;
foreach($numbers as $key => $value) {
if ($total >= $target) {
break;
}
$total += $value;
}
echo "total is $total and count is $key";
$count=0;
$target=9;
$total=0;
foreach($key=>$value的数字){
如果($total<$target){
$total=$total+$value;
$count++;
}
其他的
{
打破
}
}
echo“总计为$total,计数为$count”;
UPD:重写代码以避免未使用的循环条目出现中断如果每个循环的键等于且大于$target中断,则可以检查您的
$key
。像这样的-
$target = 9;
$total = 0;
foreach($numbers as $key => $value) {
if ($total >= $target) {
break;
}
$total += $value;
}
echo "total is $total and count is $key";
<?php
$numbers = Array ( 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 6, 5 => 1, 6 => 5.5, 7 => 1, 8 => 1, 9 => 1, 10 => 1, 11 => 1, 12 => 1, 13 => 11 );
$count=0;
$target=9;
$total=0;
foreach($numbers as $key => $value) {
if ($key >= $target) {
break;
}
$total += $value;
$count++;
}
echo "total is $total and count is $count";
?>
希望这是你真正想要的结果。(y) .如果key等于且大于
$target
则可以对每个循环进行检查$key
。像这样的-
<?php
$numbers = Array ( 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 6, 5 => 1, 6 => 5.5, 7 => 1, 8 => 1, 9 => 1, 10 => 1, 11 => 1, 12 => 1, 13 => 11 );
$count=0;
$target=9;
$total=0;
foreach($numbers as $key => $value) {
if ($key >= $target) {
break;
}
$total += $value;
$count++;
}
echo "total is $total and count is $count";
?>
希望这是你真正想要的结果。(y) .什么是
$outgoing
?为什么在foreach
中有而不是时,会有一个而不是$outgoing
编辑错误,说$target而不是$outgoing什么是$outgoing
?为什么在您的foreach
中有而呢?可能您想使用$target
而不是$outgoing
编辑错误来表示$target而不是$outgoing您需要在某个地方增加$total
,否则它将保持在0。是的,这是just if语句的示例,但这正是重点,我将把这个添加到代码中,你需要在某个地方增加$total
,否则它将保持在0.yea,这是just if语句的示例,但这就是重点,我将把它添加到代码中还想提到的是,在这种情况下,解决方案将是数组键独立的,只要数组值是数字。还想提到的是,在这种情况下,解决方案将是数组键独立的,只要数组值是数字。可能需要添加一些解释,或某种类型的文本。可能需要添加一些解释,或某种类型的文本。