0

我将尝试解释这一点而不会引起任何混淆,首先是相关代码:

$week_start_dates = array($start_of_last_week, $start_of_last_week_last_year, $start_of_previous_week);
$week_end_dates   = array($end_of_last_week, $end_of_last_week_last_year, $end_of_previous_week);
$weekname         = array('TW', 'LY', 'PW');

基本上,这些数组包含的是以下查询的开始日期和结束日期:

$leads = array();

for ($i = 0; $i < sizeof($week_start_dates) ; $i++)
{
        $sql = '
    SELECT
        email.country,
            email_dailytotal.summarydate,   
            sum(email_dailytotal.conversions) as leads      
        FROM email_dailytotal
            left join email on email.email_id = email_dailytotal.email_id
            left join email_type on email.type = email_type.type_id
        WHERE                
            email_dailytotal.summarydate >= "' . $week_start_dates[$i] . '" and email_dailytotal.summarydate <= "' . $week_end_dates[$i]  . '" and email.business = "' . $business .'" and email.business_division = "' . $businessdivision .'"
    GROUP BY email.country
    ';

$dbresult = mysql_query($sql);

while($results = mysql_fetch_array($dbresult)) {
        $country        = $results['country'];
        $no_leads       = $results['leads'];

        array_push($leads, array(
            $country => array(
               $weekname[$i]=>$no_leads
            )
        ));
}

输出如下:

[0] => Array
    (
        [uk] => Array
            (
                [TW] => 10334
            )

    )

[1] => Array
    (
        [de] => Array
            (
                [TW] => 315
            )

    )

 [2] => Array
    (
        [uk] => Array
            (
                [LY] => 18579
            )

    )

[3] => Array
    (
        [de] => Array
            (
                [LY] => 401
            )

    )

    [4] => Array
    (
        [uk] => Array
            (
                [PW] => 13410
            )

    )


etc...

当我喜欢它时:

[0] => Array
    (
        [uk] => Array
            (
                [TW] => 10334
                [LY] => 18579
                [PW] => 13410
            )

    etc...

    )
4

2 回答 2

1

尝试将其更改为:

$leads[$country] = array(
           $weekname[$i]=>$no_leads
        );
于 2012-05-25T13:32:46.443 回答
1
while($results = mysql_fetch_array($dbresult)) {
        $country        = $results['country'];
        $no_leads       = $results['leads'];

        if( !isset($leads[$country]) ){
            $leads[$country] = array();
        }
        $leads[$country][$weekname[$i]] = $no_leads;
}
于 2012-05-25T13:37:13.413 回答