使用数组值PHP MYSQL处理foreach或for循环

时间:2014-05-10 11:49:17

标签: php arrays loops for-loop foreach

输出数组(变量名称为$ r)

    Array
    (
    [Jan-14] => 7588793.52
    [Feb-14] => 9944970.87
    [Mar-14] => 8567790.20
    [Apr-14] => 
    [May-14] => 
    [Jun-14] => 
    [Jul-14] => 
    [Aug-14] => 
    [Sep-14] => 
    [Oct-14] => 
    [Nov-14] => 
    [Dec-14] => 
    )

到目前为止,我所做的是......

   while($r = mysql_fetch_assoc($query)) {
    $series1['data'][] = $r['Jan-14'];
    $series2['data'][] = $r['Feb-14'];
    .... it will go till Dec-14.......
    array_push($result,$series1);
    array_push($result,$series2);
    .... it will go till Dec-14.......
}

预期产出:

代码看起来像这样(动态)

  while($r = mysql_fetch_assoc($query)) {

        for($i=1;$i<=count($r);$i++){

        $series.$i['data'][] =  ??????????
        array_push($result,$series.$i);
        ..................

        }

}

帮帮我。不要谈论db结构或规范化。数据库由我的客户提供。

谢谢, Kimz

1 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

$result = array();
foreach($r as $month => $value) {
    $result[] = array('data' => array($value));
}