Double Foreach 意外结果
我有两个数组,都是关联的;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设所有水果都在“有序”数组中,您可以尝试这样的操作:
您的结果数组将以水果为键,并包含食用的绝对量和百分比。
Assuming all fruits are in the "ordered" array, you could try something like this:
Your result array would be keyed on fruit and have both the absolute amount eaten and the percentage.
你应该添加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.