如何从数组值

时间:2016-06-17 04:17:54

标签: php arrays json google-visualization

好的,我现在陷入困境,我试图制作一个图表来预测我的客户多年来的行为,所以我在2013年至今的数据库中都有记录。我创建了一个查询数据库的函数,并返回一个查询年份为12个月的数组,并且我通过多年的记录循环该函数,以避免在明年修改代码。我的问题是我传递给图表的数据绘制了整个循环数据,我需要在新的记录年开始时创建一个新的数据集或第二行。我的数组看起来像这样:

...
11 => 
    array (size=3)
      'year' => int 2013
      'month' => string 'Dec' (length=3)
      'total' => int 33

array (size=12)
  0 => 
    array (size=3)
      'year' => int 2014
      'month' => string 'Jan' (length=3)
      'total' => int 41
  ...

这是我到目前为止的代码:

$table['cols'] = array(

    array('label' => 'Month', 'type' => 'string'),
    array('label' => '2013', 'type' => 'number'),
    array('label' => '2014', 'type' => 'number'),
    array('label' => '2015', 'type' => 'number'),
    array('label' => '2016', 'type' => 'number')

);

$rows_graph = array();
$i = 0;
// loop for the customers list
while ($rows = mysqli_fetch_object($customers)) {

    echo 'Loop1 - ' . $rows->channel . '<br /><br />';

    // next loop for each year of records
    foreach ($years as $items) {

        echo 'Year: '.$items['year'].'<br />';


        // function(dbcon,year,customer) return an array of 12 months (ex. year => 2013, month => Jan, total = 10)
        // with the records per month for the current customer in that year.
            $data = chart_values($dbcon, $items['year'], $rows->channel);
            var_dump($data);

            // now i loop for each month
            foreach ($data as $key => $val) {
               // var_dump($test);
               // echo $val['year'] . ' - ' .$val['month'] . ' - ' . $val['total'] . '<br />';
                $temp = array();

                $temp[] = array('v' => (string)$val['month']);

                // Values of each line
                $temp[] = array('v' => (int)$val['total']);

                $rows_graph[] = array('c' => $temp);


            }

    }
    $table['rows'] = $rows_graph;
    $jsonTable = json_encode($table);
    echo $jsonTable;

    //////////////////////// graphic //////////////////////////

结果图表显示在一行中,如下所示:

Issue Snapshot

我希望获得一个看起来像这样的图表(当然有我自己的数据,行数年,只有12个月和月总数):

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我能够实现我想要的,也许是不干净但是工作,这就是我所做的:

// loop for the customers list
while ($rows = mysqli_fetch_object($customers)) {

    echo 'Loop1 - ' . $rows->customer . '<br /><br />';

//cleaning arrays

    $rows_graph = array();
    $jan = array();
    $feb = array();
    $mar = array();
    $apr = array();
    $may = array();
    $jun = array();
    $jul = array();
    $aug = array();
    $sep = array();
    $oct = array();
    $nov = array();
    $dec = array();
    $data = array();
    $table = array();

    $table['cols'] = array(
        array('label' => 'Month', 'type' => 'string'),
    );

    // next loop for each year of records
    foreach ($years as $items) {

        // echo 'Year: '.$items['year'].'<br />';
        $table['cols'][] = array('label' => $items['year'], 'type' => 'number');

        // function(dbcon,year,customer) return an array of 12 months (ex. year => 2013, month => Jan, total = 10)
        // with the records per month for the current customer in that year.
        $data[] = chart_values($dbcon, $items['year'], $rows->channel);
        //var_dump($data);


    }
    // here is build the array for the chart
    //var_dump($data);

    $jan[] = array('v' => (string)'Jan');
    $feb[] = array('v' => (string)'Feb');
    $mar[] = array('v' => (string)'Mar');
    $apr[] = array('v' => (string)'Apr');
    $may[] = array('v' => (string)'May');
    $jun[] = array('v' => (string)'Jun');
    $jul[] = array('v' => (string)'Jul');
    $aug[] = array('v' => (string)'Aug');
    $sep[] = array('v' => (string)'Sep');
    $oct[] = array('v' => (string)'Oct');
    $nov[] = array('v' => (string)'Nov');
    $dec[] = array('v' => (string)'Dec');


    foreach ($data as $value) {
       // var_dump($value);
        foreach ($value as $key => $val) {
            // var_dump($val);
            // echo $val['year'] . ' - ' . $val['month'] . ' - ' . $val['total'] . '<br />';
            //$temp = array();
            //$temp[] = array('v' => (string)$val['month']);

            if ($val['month'] == 'Jan') {
                $jan[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Feb') {
                $feb[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Mar') {
                $mar[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Apr') {
                $apr[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'May') {
                $may[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Jun') {
                $jun[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Jul') {
                $jul[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Aug') {
                $aug[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Sep') {
                $sep[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Oct') {
                $oct[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Nov') {
                $nov[] = array('v' => (int)$val['total']);
            }
            if ($val['month'] == 'Dec') {
                $dec[] = array('v' => (int)$val['total']);
            }

        }
    }


    $rows_graph[] = array('c' => $jan);
    $rows_graph[] = array('c' => $feb);
    $rows_graph[] = array('c' => $mar);
    $rows_graph[] = array('c' => $apr);
    $rows_graph[] = array('c' => $may);
    $rows_graph[] = array('c' => $jun);
    $rows_graph[] = array('c' => $jul);
    $rows_graph[] = array('c' => $aug);
    $rows_graph[] = array('c' => $sep);
    $rows_graph[] = array('c' => $oct);
    $rows_graph[] = array('c' => $nov);
    $rows_graph[] = array('c' => $dec);

    $table['rows'] = $rows_graph;
    $jsonTable = json_encode($table);
    //var_dump($table);
    //echo $jsonTable;


    //////////////////////// graphic //////////////////////////