我怎样才能重构这只可怕的野兽呢?

时间:2016-12-21 16:50:26

标签: php arrays object refactoring

我有一个我执行的查询,然后我使用

while($row = $result->fetch_assoc()){
        $data[] = $row;

我知道这是怪诞的,但我不确定如何使这个对象或多维数组,所以我只是做了这样的事情......

$NUH2016 = array();
$NUH2017 = array();
$NUH2018 = array();
$NUH2019 = array();
$NUH2020 = array();
$NUH2021 = array();
$RBN2016 = array();
$RBN2017 = array(); ...

...$GDT2019 = array();
$GDT2020 = array();
$GDT2021 = array();

while($row = $result->fetch_assoc()){
        $data[] = $row;
        if($row['Location'] == 'NUH' && $row['Year'] == '2016'){
            $NUH2016[] = $row['P_1'];
            $NUH2016[] = $row['P_2'];
            $NUH2016[] = $row['P_3'];
            $NUH2016[] = $row['P_4'];
        }
        if($row['Location'] == 'NUH' && $row['Year'] == '2017'){
            $NUH2017[] = $row['P_1'];
            $NUH2017[] = $row['P_2'];
            $NUH2017[] = $row['P_3'];
            $NUH2017[] = $row['P_4'];
        }
        //...for all locations and years

 ...then
 $content = array();
$results = array();

$NUH2016_total = array_sum($NUH2016);
$NUH2017_total = array_sum($NUH2017);
$NUH2018_total = array_sum($NUH2018);
$NUH2019_total = array_sum($NUH2019);
$NUH2020_total = array_sum($NUH2020);
$NUH2021_total = array_sum($NUH2021);


$results['NUH~2016'] = $NUH2016_total;
$results['NUH~2017'] = $NUH2017_total;
$results['NUH~2018'] = $NUH2018_total;
$results['NUH~2019'] = $NUH2019_total;
$results['NUH~2020'] = $NUH2020_total;
$results['NUH~2021'] = $NUH2021_total;

$RBN2016_total = array_sum($RBN2016);
$RBN2017_total = array_sum($RBN2017);
$RBN2018_total = array_sum($RBN2018);
$RBN2019_total = array_sum($RBN2019);
$RBN2020_total = array_sum($RBN2020);
$RBN2021_total = array_sum($RBN2021);


$results['RBN~2016'] = $RBN2016_total;
$results['RBN~2017'] = $RBN2017_total;
$results['RBN~2018'] = $RBN2018_total;
$results['RBN~2019'] = $RBN2019_total;
$results['RBN~2020'] = $RBN2020_total;
$results['RBN~2021'] = $RBN2021_total;
...etc

$content['Results'] = $results;
$response = json_encode($content);
echo $response;

所以我想做的是创建一个看起来像这样的对象或关联数组。

content {
        produced hours{   
                       nuh{ 2016:16,000
                            2017:8,000
                            ...
                           }
                       rbn{ 2016:9,000
                            2017:whatever
                            ....
                      }
           man hours{
                       nuh{ 2016:4,000
                            2017:2,000
                            ...
                           }
                       rbn{ 2016:1,000
                            2017:more stuff
                            ....
                      }

但我对php对象很陌生。我怎样才能重构它以便我不必初始化每个空数组,有一堆if语句使查询中的每个条目都适合一个数组进行求和,然后将它全部放入一个对象中最后从ajax成功函数中恢复过来?

3 个答案:

答案 0 :(得分:1)

您可以拥有一系列数组,因此您无需手动分配所有数组。

$LocationAndYears;
while($row = $result->fetch_assoc())
{
    $LocationAndYears[$row['Location']] [$row['Year']] [0] = $row['P_1'];
    $LocationAndYears[$row['Location']] [$row['Year']] [1] = $row['P_2'];
    $LocationAndYears[$row['Location']] [$row['Year']] [2] = $row['P_3'];
    $LocationAndYears[$row['Location']] [$row['Year']] [3] = $row['P_4'];
    $LocationAndYears[$row['Location']] [$row['Year']] ['sum'] = array_sum($LocationAndYears[$row['Location']] [$row['Year']]);
}
echo $LocationAndYears['NUH']['2012'][0];
echo "Sum for 2012 at NUH:" . $LocationAndYears['NUH']['2012']['sum'];
print_r($LocationAndYears);

答案 1 :(得分:1)

没有必要声明这么多数组。只需按以下方式更改while()循环,

$resultArr = array();
while($row = $result->fetch_assoc()){
    $tmpArr = array($row['P_1'], $row['P_2'], $row['P_3'], $row['P_4']);
    $sumTotal = array_sum($tmpArr);
    $tmpArr['sumTotal'] = $sumTotal; 
    $resultArr[$row['Location']][$row['Year']][] = $tmpArr;
}

稍后,在结果数组上应用json_encode(),如下所示,

$json = json_encode($resultArr);

Sidenote :如果您想查看完整的数组结构,请执行var_dump($resultArr);

答案 2 :(得分:0)

无需编写全部内容,请查看多维数组。 这样的事可能...... 但实际上我认为这个问题的答案是指向多维数组,因为它很模糊

$data = array();
while($row = $result->fetch_assoc()){
    $location   = $row['Location'];
    $year       = $row['Year'];

    $data[$location] = $data[$location] ? $data[$location] : array();
    $data[$location][$year] = $data[$location][$year] ? $data[$location][$year] : array();

    $data[$location][$year][] = $row['P_1'];
    $data[$location][$year][] = $row['P_2'];
    $data[$location][$year][] = $row['P_3'];
    $data[$location][$year][] = $row['P_4'];
}