2016-09-10 3 views
0

週数と季節にポイントがあるJSON配列(多かれ少なかれ入力可能)から計算スクリプトを作成しようとしています。合計を出力しますが、50を超えないようにしてください。 私の結果が0であるところで私は間違いを犯します。私はPHPにとって非常に新しいので、このスクリプトはすでに私にいくつかの時間を取っていました。間違っている?json配列の計算スクリプトを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

私は間違っているかもしれないが、もし、ループはこのように見てはいけませんか?

if(!empty($experiences)) { 
    foreach($experiences as $experience) { 
関連する問題