用新值替换多维数组值

时间:2014-10-23 18:34:52

标签: php arrays performance multidimensional-array

目前我有这个工作代码,它将从旧的多维数组中替换值,并从db获取新值。首先我声明数组为12个月。代码如下:

$base = Array(
    Array
        (
            "month" => '2010-01',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),

    Array
        (
            "month" => '2010-02',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),

    Array
        (
            "month" => '2010-03',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ), 

    Array
        (
            "month" => '2010-04',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               

    Array
        (
            "month" => '2010-05',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               

    Array
        (
            "month" => '2010-06',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               

    Array
        (
            "month" => '2010-07',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),


    Array
        (
            "month" => '2010-08',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               

    Array
        (
            "month" => '2010-09',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               


    Array
        (
            "month" => '2010-10',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),

    Array
        (
            "month" => '2010-11',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),   

    Array
        (
            "month" => '2010-12',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
         )                                         
    );

$result = mysqli_query($con, "SELECT brand, 
DATE_FORMAT( date_time, '%Y' ) AS 'year', 
DATE_FORMAT( date_time, '%m' ) AS 'month', 
SUM(brand='PZH') AS PZH,
SUM(brand='KFC') AS KFC,
SUM(brand='SUB') AS SUB
FROM scan_report WHERE DATE_FORMAT( date_time, '%Y' ) =2010 AND id = ".$_SESSION['userinfo']['id']."
GROUP BY DATE_FORMAT( date_time, '%Y%m' )") or die(mysqli_error($con));
$num_rows = 1;  

while ($row = mysqli_fetch_array($result)) {
foreach($base as &$value) {

    echo $num_rows;     
    if ($value['month'] == $row['year'].'-'.$row['month']){     
        $value['KFC'] = $row['KFC'];
        $value['PZH'] = $row['PZH'];
        $value['SUB'] = $row['SUB'];

     }

}
$num_rows++;
}   

因为多维数组,for each循环了12次。在回声期间,当从db查询时,有5个匹配记录的60个循环。有没有比我现在做的更优雅的解决方案?有什么想减少循环吗?

3 个答案:

答案 0 :(得分:1)

您最好的选择是获取month值并将其用作数组中的键:

$base = Array(
    '2010-01' => Array
        (
            "month" => '2010-01',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),
     // ... and so on
    );

然后你可以这样搜索:

while ($row = mysqli_fetch_array($result)) {
    $searchKey = $row['year'].'-'.$row['month'];
    if (isset($base[$searchKey])) {
        $base[$searchKey]['KFC'] = $row['KFC'];
        $base[$searchKey]['PZH'] = $row['PZH'];
        $base[$searchKey]['SUB'] = $row['SUB'];
    } else {
        $base[$searchKey] = array(
            'KFC' => $row['KFC'],
            'PZH' => $row['PZH'],
            'SUB' => $row['SUB'],
        );
    }
    $num_rows++;
}   

答案 1 :(得分:0)

我不会预定义数组值,而是希望使用年月作为关键字(它是唯一的,因为它按此分组)。

$base = array();
while($row = mysqli_fetch_array($result)) {
    echo $num_rows;
    $key = $row['year'].'-'.$row['month'];
    $base[$key] = array();
    $base[$key]['KFC'] = $row['KFC'];
    $base[$key]['PZH'] = $row['PZH'];
    $base[$key]['SUB'] = $row['SUB'];
    $num_rows++;
}

答案 2 :(得分:0)

我喜欢在这种情况下使用关联多维数组。例如,您可以按如下方式排列基本数组:

$base = array('2010-01' => array(
        "KFC" => '0',
        "PZH" => '0',
        "SUB" => '0'
    ),
    '2010-01' => array(
        "month" => '',
        "KFC" => '0',
        "PZH" => '0',
        "SUB" => '0'
    ) //etc
    );

然后,当您从数据库处理结果时,您可以访问$ base数组的特定部分,如:

while ($row = mysqli_fetch_array($result)) {

$base[$row['year'] . '-' . $row['month']]['KFC'] = $row['KFC'];
$base[$row['year'] . '-' . $row['month']]['PZH'] = $row['pZH'];
$base[$row['year'] . '-' . $row['month']]['SUB'] = $row['SUB'];

}

然后,您只需遍历数据库行即可实现更新过程。有意义吗?