簡體   English   中英

用於谷歌圖表的 PHP 數組; 循環遍歷數組並按 2 分組

[英]PHP array for google chart; loop thru array and group by 2

(在聖誕大餐之間)我再次陷入數組和邏輯分組的循環中。 這是我得到的數組

$aTest = Array
(
    Array
        (
            'date' => 2017-05-04,
            'period' => 'period2',
            'category' => 'Indoor room',
            'score' => 1
        ),

    Array
        (
            'date' => 2017-05-04,
            'period' => 'period5',
            'category' => 'Indoor room',
            'score' => 1
        ),

    Array
        (
            'date' => 2017-05-04,
            'period' => 'period2',
            'category' => 'Indoor room',
            'score' => 2
        ),

    Array
        (
            'date' => 2017-05-04,
            'period' => 'period4',
            'category' => 'Indoor room',
            'score' => 1
        ),

     Array
        (
            'date' => 2017-05-03,
            'period' => 'period5',
            'category' => 'Gym Class',
            'score' => 1
        ),

    Array
        (
            'date' => 2017-05-03,
            'period' => 'period1',
            'category' => 'Gym Class',
            'score' => 1
        ),

     Array
        (
            'date' => 2017-05-03,
            'period' => 'period4',
            'category' => 'Indoor room',
            'score' => 1
        )
        );

這次我喜歡按類別分組並按時期分組總分。 Y 軸是類別,X 軸是周期。 最后我需要這個作為谷歌圖表

/*period, total indoor, total gym, 'total indoor', 'total gym'*/
array(
['Period1', 0,1,'0','1'],
['Period2', 3,0,'3','0'],
['Period3', 0, 0,'0','0'],
['Period4', 4,0,'4','0'],
['Period5', 1,1,'1','1']
 )

我有這個 php 代碼:

    foreach ($aTest as $value) {
            //echo $value['period'].' - '.$value['score'].'<br/>';

            if (empty($output[$value]['period']))
                $output[$value]['period'] = ['Period1' => 0, 'Period2' => 0, 'Period3' =>0, 'Period4' => 0, 'Period5' => 0];

   if(empty($output[$value]['category']))
        $output[$value['catgeory']] = ['Gym Class' => 0, 'Indoor room' =>0];

            $output[$value['category']] += $value['score'];
        }

        ksort($output);

但這只是按類別而不是按時間段來計算總分。 我想我也需要遍歷周期,但是如何?

  • 你這里有錯誤的邏輯。
 if (empty($output[$value]['period'])) $output[$value]['period'] = ['Period1' => 0, 'Period2' => 0, 'Period3' =>0, 'Period4' => 0, 'Period5' => 0];

$value是一個數組,您嘗試檢查$output[$value]

  • 我看到您沒有任何行總和周期值。

  • 我有你的數據的解決方案。 我的代碼是做什么的??

  • 類別分類時期總分

  • 對於每個合並周期類別分數到一個數組,使用arraySort category 來設置這些類別分數值的位置
$temp = []; $output = []; foreach($aTest as $value){ $period = $value['period']; $category = $value['category']; // Create default values if (empty($temp[$period])){ $temp[$period] = []; } if(empty($temp[$period][$category])){ $temp[$period][$category] = 0; } //Sum score $temp[$period][$category] += $value['score']; } //After Forech we have an array with ['period name' => ['category name' => score]]; //Sort values of the category change it if you want, you can add more option such as (item type for add '' to values) $arraySort = [ "Indoor room", //total indoor, "Gym Class", // total gym, "Indoor room", //'total indoor', "Gym Class" //'total gym' ]; foreach($temp as $period => $catsScore){ $periodItem = [$period]; foreach($arraySort as $cat){ $periodItem[] = $catsScore; } $output[] = $periodItem; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM