试图在php创建一个json数组的计算脚本


Trying to create a calculation script of a json array in php

我试图从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;

我可能错了,但是if和循环不应该是这样的吗?

if(!empty($experiences)) {
    foreach($experiences as $experience) {