php - 表中的当年数据按月记录

标签 php javascript mysql

我有这个查询,运行良好......

 $result        =   mysql_query(SELECT Month(ord_aff_date) AS saleMonth, ROUND(SUM(ord_aff_amount),0) AS Total 
                FROM order_affiliate_details 
                WHERE aff_id='12' AND YEAR(ord_aff_date)=YEAR(CURDATE())
                GROUP BY Month(ord_aff_date) ORDER BY Month(ord_aff_date)");

while($datas    =   mysql_fetch_array($result)){
    $months[]=$datas['saleMonth'];
    $value[]=   $datas['Total'];
}

print_r($months); exit;

下面给出的结果(并非所有月份)显示......这是正确的......

 Result => Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 ) 

现在我想获取一组值,就像这个。

Array(['January', 437], ['February', 322], ['March', 233], ['April', 110], ['May', 34], ['June', 20], ['July', 19], ['August', 110], ['September', 34], ['November', 20], ['December', 19],);

作为 js 数组...但现在我不知道该怎么做......任何人都可以帮助我......??

解决方案:

// As i want to get these data as a json encoded and also in another array, 
// but with out square brackets at the end.. so now i find the solution which 
// is fit in as js library want...

$final = array_combine($arr_monthName,$values);
    $allMonths = array();
    $i=0;
    foreach($final as $k=>$v)
    {   
        $allMonths[$i]=array($k,$v);
        $i++;
    } 

    // then do json_encode
    $json = json_encode($allMonths);
    $json = preg_replace('/^\[|\]$/', '', $json);

 // answer return as....
 => ['January', 437], ['February', 322], ['March', 233], ['April', 110], ['May', 34]

 // which then i pass to a js array.... simply.
 var arrayData = new Array(<?=$json?>);

 // many many thanks to diEcho, which help me to get this... thanks.

最佳答案

如果我理解你的问题,那么我认为你目前得到的输出是这样的

$output = array(0=>array('saleMonth'=>'January','total=>437),
                1=>array('saleMonth'=>'February','total=>322). ... )

更新

// first create an array having key as month name and value as total
// suppose we have below array

$monthArray = array( 0=>2, 1 =>3, 2=> 4, 3=>5 );
$valueArray = array( 0=>437,1=>322, 2=>233,4=>458);
function intToMonth($monthNumber)
{
    $strTime=mktime(1,1,1,$monthNumber,1,date("Y"));
    return date("F",$strTime);  
}
$arr_monthName= array_map('intToMonth',$monthArray);    
$final = array_combine($arr_monthName,$valueArray);
//final array would be 

 Array
(
    [February] => 437
    [March] => 322
    [April] => 233
    [May] => 458
)
// then do json_encode
json_encode($final);
//output would be in below format, Associative array always output as object:
{"February":437,"March":322,"April":233,"May":458}

更多更新

要将输出作为数组的数组,请尝试

$new = array();
$i=0;
foreach($final as $k=>$v)
{   
    $new[$i]=array($k,$v);
    $i++;
}
json_encode($new);
//output would be in below format , array of array
[["February",437],["March",322],["April",233],["May",458]]

<强> Reference

关于php - 表中的当年数据按月记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6043039/

相关文章:

javascript - PHP/HTML 模式警报

javascript - 使用 javascript 从具有相同类的元素克隆内容

javascript - Netbeans 的 Javascript/Jquery Intellisense 问题

mysql - posts_per_page 设置为 1,返回 50 个帖子

php - MySQL 通过 LOAD DATA INFILE 从 csv 文件导入数据不起作用

php - 在整数上调用成员函数 toSql()

php - 从子查询绑定(bind)数据?

javascript - 计算相机近平面和远平面边界

mysql - 查询具有特定值的最新记录的计数

mysql - 用于索引表的 OR 和 IN 运算符的替代方案