从百分比总和中找出百分比 PHP
Find the percentage from a sum of percentages PHP
所以我有一系列子项目,它们自己的百分比为 100,而父项目是所有这些百分比的总和(满 100):
$parentItem['percent'] = sumOfAllChildren %
$childItem1['percent'] = 50
$childItem2['percent'] = 60
$childItem3['percent'] = 100
$childItem4['percent'] = 15
在这种情况下,如何使用 PHP 计算父项的总和?
您始终可以通过稍微重构代码来使用 array_sum 函数。
$children = [50,60,100,15];
$parent = array_sum($children); // would give you 225
// add a child
$children[] = 100;
$parent = array_sum($children); // would give you 325
有关详细信息,请参阅 http://php.net/manual/en/function.array-sum.php。
这样计算:
$children = [$childItem1, $childItem2, $childItem3, $childItem4];
$childPers = array_column($children, 'percent');
$parentItem['percent'] = array_sum($childPers)/(100 * count($childPers));
希望有用
$childItem1['percent'] = 50;
$childItem2['percent'] = 60 ;
$childItem3['percent'] = 100 ;
$childItem4['percent'] = 15 ;
$total=0;
$total+=$childItem1['percent'];
$total+=$childItem2['percent'];
$total+=$childItem3['percent'];
$total+=$childItem4['percent'];
$percent=(100 / 400) * $total;
$parentItem['percent'] = $percent. "%";
谢谢..
所以我有一系列子项目,它们自己的百分比为 100,而父项目是所有这些百分比的总和(满 100):
$parentItem['percent'] = sumOfAllChildren %
$childItem1['percent'] = 50
$childItem2['percent'] = 60
$childItem3['percent'] = 100
$childItem4['percent'] = 15
在这种情况下,如何使用 PHP 计算父项的总和?
您始终可以通过稍微重构代码来使用 array_sum 函数。
$children = [50,60,100,15];
$parent = array_sum($children); // would give you 225
// add a child
$children[] = 100;
$parent = array_sum($children); // would give you 325
有关详细信息,请参阅 http://php.net/manual/en/function.array-sum.php。
这样计算:
$children = [$childItem1, $childItem2, $childItem3, $childItem4];
$childPers = array_column($children, 'percent');
$parentItem['percent'] = array_sum($childPers)/(100 * count($childPers));
希望有用
$childItem1['percent'] = 50;
$childItem2['percent'] = 60 ;
$childItem3['percent'] = 100 ;
$childItem4['percent'] = 15 ;
$total=0;
$total+=$childItem1['percent'];
$total+=$childItem2['percent'];
$total+=$childItem3['percent'];
$total+=$childItem4['percent'];
$percent=(100 / 400) * $total;
$parentItem['percent'] = $percent. "%";
谢谢..