Double Foreach 意外结果

发布于 2024-12-18 15:12:09 字数 1569 浏览 0 评论 0原文

我有两个数组,都是关联的;

ArrayUneaten ( lemon=> 7, banana=>6, apple=>10)//the units of UNeaten fruit
ArrayOrdered (lemon =>10, strawberry =>10, Kiwi=>0, Apple=>20, Banana=>6) // the units of ordered fruit

我想创建第三个数组(如果顺序与 ArrayOrdered 相同,数字就可以) 所有水果选项的数量以及吃水果的百分比(与订购的水果相比)。

(由于其他原因,如果订购值= 0,%吃= 0%,请注意)

(注意2,请注意,草莓所需百分比为100,没有未吃的草莓,订购了10个,因此已吃掉10个)

所以所需的数组

ArrayEatenPercentage (70, 100, 0, 50, 0)

我的编码尝试

$CompletedPercentagesArray = array ();

foreach( $ArrayOrdered as $fruitOrdered => $unitsOrdered) {
    if ($unitsOrdered == 0){
        //if it's zero it's never been selected
        $completedPercentage = 0;
    }
    foreach( $ArrayUneaten as $fruitUneaten => $unitsUneaten) {
        if ( $fruitUneaten == $fruitOrdered){
            // ($totalCardsChosen = $timesSelected*25; - please ignore)
            $percentageUneaten = 100*($fruitUneaten/$unitsOrdered);
            $percentageEaten = 100 - $percentageUneaten;
            $completedPercentage = round ($percentageEaten, 1);
        }
        else {//if this is true then it's been selected and been finished
            $completedPercentage = 100;
        }
    }
    array_push( $completedPercentagesArray, $completedPercentage ); //this adds the variable to the array
}
print_r($completedPercentagesArray);

当前输出是意外的。

仅正确处理 ArrayUneaten 的最后一个值。

其他值返回 100%。

ArrayEatenPercentage (100, 100, 100, 50, 100)

I have two arrays, both associative;

ArrayUneaten ( lemon=> 7, banana=>6, apple=>10)//the units of UNeaten fruit
ArrayOrdered (lemon =>10, strawberry =>10, Kiwi=>0, Apple=>20, Banana=>6) // the units of ordered fruit

I want to create a third array (numeric is fine if in the same order as ArrayOrdered)
of all fruit options and the % of Fruit Eaten (compared to Fruit Ordered).

(NB for other reasons if Ordered Value = 0, %eaten = 0%)

(NB2, note that % desired for strawberries is 100, no strawberries are uneaten, 10 ordered, therefore 10 have been eaten)

So desired Array

ArrayEatenPercentage (70, 100, 0, 50, 0)

My coding attempt

$CompletedPercentagesArray = array ();

foreach( $ArrayOrdered as $fruitOrdered => $unitsOrdered) {
    if ($unitsOrdered == 0){
        //if it's zero it's never been selected
        $completedPercentage = 0;
    }
    foreach( $ArrayUneaten as $fruitUneaten => $unitsUneaten) {
        if ( $fruitUneaten == $fruitOrdered){
            // ($totalCardsChosen = $timesSelected*25; - please ignore)
            $percentageUneaten = 100*($fruitUneaten/$unitsOrdered);
            $percentageEaten = 100 - $percentageUneaten;
            $completedPercentage = round ($percentageEaten, 1);
        }
        else {//if this is true then it's been selected and been finished
            $completedPercentage = 100;
        }
    }
    array_push( $completedPercentagesArray, $completedPercentage ); //this adds the variable to the array
}
print_r($completedPercentagesArray);

Current output is unexpected.

Only last value of ArrayUneaten is processed correctly.

The other values return 100%.

ArrayEatenPercentage (100, 100, 100, 50, 100)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜嗑 2024-12-25 15:12:09

假设所有水果都在“有序”数组中,您可以尝试这样的操作:

$results = array();
foreach( $arrayOrdered as $fruit => $numOrdered ) {
    if( Isset( $arrayUneaten[$fruit] ) ) {
        $numEaten = $numOrdered - $arrayUneaten[$fruit];
    } else {
        $numEaten = $numOrdered;
    }
    if( $numOrdered > 0 ) {
        $percentEaten = $numEaten / $numOrdered * 100;
    } else {
        $percentEaten = 0;
    }
    $results[$fruit] = array( 'eaten' => $numEaten, 'percent' => $percentEaten );
}

您的结果数组将以水果为键,并包含食用的绝对量和百分比。

Assuming all fruits are in the "ordered" array, you could try something like this:

$results = array();
foreach( $arrayOrdered as $fruit => $numOrdered ) {
    if( Isset( $arrayUneaten[$fruit] ) ) {
        $numEaten = $numOrdered - $arrayUneaten[$fruit];
    } else {
        $numEaten = $numOrdered;
    }
    if( $numOrdered > 0 ) {
        $percentEaten = $numEaten / $numOrdered * 100;
    } else {
        $percentEaten = 0;
    }
    $results[$fruit] = array( 'eaten' => $numEaten, 'percent' => $percentEaten );
}

Your result array would be keyed on fruit and have both the absolute amount eaten and the percentage.

野侃 2024-12-25 15:12:09

你应该添加break;在 if ( $fruitUneaten == $fruitOrdered){ 条件块的末尾。否则,除非当前水果是 $ArrayUneaten 的最后一个元素(即苹果),否则 $completedPercentage 始终为 100,因为在内循环的最后一次迭代中,异常会转到 else 块。

You should have added break; at the end of if ( $fruitUneaten == $fruitOrdered){ condition block. Otherwise unless the current fruit is the last element(which is apple) of $ArrayUneaten, $completedPercentage becomes always 100, because on the last iteration of the inner loop exection goes to that else block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文