title: How to dynamically populate a google stacked bar chart from an array using php?
body: <p>I would like to use a google stacked bar chart.
  Manually it would be just like that.</p>
  
  <pre><code>&lt;script type="text/javascript" src="https://www.google.com/jsapi"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
        google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
  
        var data = google.visualization.arrayToDataTable([
          ['Year', 'A', 'B', 'C', { role: 'annotation' } ],
          ['2011', 2, 1, 1, ''],
          ['2012', 1, 0, 0, ''],
          ['2013', 0, 0, 1, ''],
          ['2014', 1, 1, 1, ''],
          ['2015', 1, 0, 0, '']
    ]);
  
        var options = {
        bar: { groupWidth: '64%' },
        chartArea: {width:'78%',height:'86%'},
        forceIFrame: 'true',
            isStacked: true,
        legend: {position: 'none'},
            titlePosition: 'none',
        vAxis: {title: 'Technological Fields'}
        };
  
    var chart = new google.visualization.ColumnChart(document.getElementById('technological-fields'));
    chart.draw(data, options);
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>But the values of 'var data' should come from an array that is generated after a data bank query.
  The original array (from the data bank) is like the following:</p>
  
  <pre><code>$limitedArray = array(
        array('A', 'B', 'G', 'D', 1, 'A', '2011-01-01'),
        array('K', 'L', 'M', 'O', 3, 'C', '2013-03-03'),
        array('K', 'L', 'M', 'O', 3, 'C', '2014-03-03'),
        array('P', 'P', 'S', 'T', 4, 'A', '2011-04-04'),
        array('A', 'B', 'G', 'D', 1, 'A', '2012-01-01'),
        array('E', 'Z', 'H', 'I', 2, 'B', '2011-02-02'),
        array('K', 'L', 'M', 'O', 3, 'C', '2011-03-03'),
        array('P', 'P', 'S', 'T', 4, 'A', '2014-04-04'),
        array('P', 'P', 'S', 'T', 4, 'B', '2014-04-04'),
        array('A', 'B', 'G', 'I', 1, 'A', '2015-05-05')
  );
  </code></pre>
  
  <p>Then I try to get some data from this:</p>
  
  <pre><code>//CHART 02
  $techArea = $limitedArray[$key][5];
  $techAreaHeader[] = $techArea;
  $prioDate = substr($limitedArray[$key][6],0,4);
  $chartContent[] = "$prioDate".","."$techArea";
  
  //Exclude duplicated rows of the header.
  $header = array();
  foreach ($techAreaHeader as &amp;$value) {
          if (!isset($header[$value['0']]))
          $header[$value['0']] =&amp; $value;
  }
  //Sort array from high to low.
  asort($header);
  
  //Transform array into string.
  $strHeader = implode(', ', $header);
  </code></pre>
  
  <p>With that I get following result:</p>
  
  <pre><code>$header:
  Array
  (
      [A] =&gt; A
      [B] =&gt; B
      [C] =&gt; C
  )
  
  $strHeader (string):
  A, B, C
  </code></pre>
  
  <p>This I could use in the first line of the 'var data' just like that:</p>
  
  <pre><code>      var data = google.visualization.arrayToDataTable([
          ['Year', &lt;?php echo $strHeader ?&gt;, { role: 'annotation' } ],
  </code></pre>
  
  <p>I'm not sure if this is the best approach (probably not), but then I've got even bigger problems when I try to populate the rest of 'var data'.
  I tried the following, but then I could not find any solution:</p>
  
  <pre><code>//Sort content array.
  asort($chartContent);
  
  //Count how many occurrences of a tech area in the same year.
  $countedChartContent = array_count_values($chartContent);
  
  $chart02 = implode(', ', array_map(function ($v, $k) { return $k.','.$v; }, $chart02, array_keys($chart02)));
  </code></pre>
  
  <p>With the following results:</p>
  
  <pre><code>$content (sorted):
  Array
  (
      [3] =&gt; 2011,A
      [0] =&gt; 2011,A
      [5] =&gt; 2011,B
      [6] =&gt; 2011,C
      [4] =&gt; 2012,A
      [1] =&gt; 2013,C
      [7] =&gt; 2014,A
      [8] =&gt; 2014,B
      [2] =&gt; 2014,C
      [9] =&gt; 2015,A
  )
  
  $countedChartContent:
  Array
  (
      [2011,A] =&gt; 2
      [2011,B] =&gt; 1
      [2011,C] =&gt; 1
      [2012,A] =&gt; 1
      [2013,C] =&gt; 1
      [2014,A] =&gt; 1
      [2014,B] =&gt; 1
      [2014,C] =&gt; 1
      [2015,A] =&gt; 1
  )
  </code></pre>
  
  <p>Yeah! I'm stucked :-\
  Did research, but could not go any further.
  Anybody has a better idea how I can populate this 'var data' dynamically from my original array?
  Many thanks in advance!!!</p>