我正在尝试从JSON数组创建一个计算脚本(可以是更多或更少的条目),其中周和季节的数量有点,并且它们应该一起输出总数,但不大于50。 在某个地方我犯了一个错误,因为我的结果是0.因为我对PHP很新,这个脚本已经花了我一些时间:-)渴望学习..有人能指出我做错了吗?
这是数组:
Array
(
[experiences] => Array
(
[0] => Array
(
[quantity] => 1
[unit] => seasons
[description] => skischool 1
)
[1] => Array
(
[quantity] => 5
[unit] => weeks
[description] => skischool 2
)
[2] => Array
(
[quantity] => 3
[unit] => seasons
[description] => skischool 3
)
[3] => Array
(
[quantity] => 2
[unit] => weeks
[description] => skischool 4
)
)
)
以下是我对剧本的看法:
$incoming = json_decode($text, true);
$experiences = Sanitize::getVar($incoming, 'experiences');
$total = 0;
$weeks_points = 0.5;
$seasons = 5;
if(!empty($experiences['experiences'])) {
foreach($experiences['experiences'] as $experience) {
if($experience['unit'] == 'seasons') {
$total = $total + ($experience['quantity'] * $sessons);
} else if($experience['unit'] == 'weeks') {
$total = $total + ($experience['quantity'] * $weeks_points);
}
}
}
$total = round($total);
echo $total = $total > 50 ? 50 : $total;
答案 0 :(得分:0)
我可能错了,但不应该if和循环看起来像这样吗?
if(!empty($experiences)) {
foreach($experiences as $experience) {