Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:51:43+00:00 2026-05-26T02:51:43+00:00

Imagine each row as X axis and each column as a series (a line

  • 0

Imagine each row as X axis and each column as a series (a line in the chart):

---------------------------------
year | apple_price | banana_price
---------------------------------
2010   2.5           1.7
2011   2.6           2.0

array // MySQL results set
  0 => 
    array
      'year' => 2010
      'apple_price' => 2.5
      'banana_price' => 1.7
  1 => 
    array
      'year' => 2011
      'apple_price' => 2.6
      'banana_price' => 2.0

I need to transform mysql array to something more suitable for my charts API and JSON. How would you implement this? Would be possible to transpose the results set? EDIT: this of course should be dynamic: first column is X axis, the remain will be the series (lines in the chart):

array // indexed
  0 => 
    array // associative
      'name' => 'apple_price'
      'data' => [[2010, 2.5], [2011, 2.6]] // indexed array of array
  1 => 
    array // associative
      'name' => 'banana_price'
      'data' => [[2010, 1.7], [2011, 2.0]] // indexed array of array

EDIT: first quick and dirty working solution, trying to get a better one:

public static function mysqlResultSetToChartSeries($data)
{
    // Return empty array if 0 rows or columns < 2
    if (!isset($data[0]) || !count($data[0]) > 1) return array();

    // Get all keys (columns names)
    $keys = array_keys($data[0]);

    // Temp array for storing col names and values
    $tmp = array();
    foreach($keys as $k) $tmp[$k] = array_map(function($r) use ($k){
        return $r[$k];
    }, $data);

    // X axis
    $x = array_shift($tmp);

    $series = array_map(function($k, $v) use ($x) {
        return array(
            'name' => $k,
            'data' => array_map(function($xaxis, $yaxis) use ($x) {
                return array($xaxis, $yaxis);
            }, array_values($x), $v)
        );
    }, array_keys($tmp), array_values($tmp));

    return $series;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:51:44+00:00Added an answer on May 26, 2026 at 2:51 am

    The class:

    <?php
    class ChartData {
    
        private $data;
        private $columns;
        private $xAxis;
    
        /**
         * @param string $xAxis The key of the array that is the x-axis.
         * @param array $columns An array of column names.
         */
        public function __construct($xAxis, array $columns) {
                $this->data = array();
            $this->xAxis = $xAxis;
    
            //we need to build an index to know how to get
            //from a key to an index.
            $i = 0;
            foreach ($columns as &$key) {
                if ($key != $xAxis) {
                    $this->columns[$key] = $i++;
                }
            }
            foreach ($this->columns as $key => &$value) {
                $this->data[$value] = array(
                    'name' => $key,
                    'data' => array()
                );
            }
        }
    
        /**
         * @param array $data An associative array mapping data names to values.  Must include the index 'year' and otherwise match $columns
             */
        public function add(array $data) {
            foreach ($data as $key => &$value) {
                if ($key != $this->xAxis) {
                    $this->data[$this->columns[$key]]['data'][] = array($data[$this->xAxis], $value);
                }
            }
        }
    
        /**
         * @return array An associative array in the format for the Charts API.
         */
        public function getArray() {
            return $this->data;
        }
    
    }
    ?>
    

    To use:

    $pricesData = array(
        array(
            'year' => '2010',
            'jamba' => '2.5',
            'juice' => '1.7'
        ),
        array(
            'year' => '2011',
            'jamba' => '2.6',
            'juice' => '2.0'
        )
    );
    
    $prices = new ChartData('year', array('year', 'jamba', 'juice'));
    foreach ($pricesData as &$priceData) {
        $prices->add($priceData);
    }
    print_r($prices->getArray());
    

    Notes:

    • My implementation requires that you tell it what the x-axis will be. You do so in the constructor
    • It is dynamic. You can have as many columns as you want..
    • It’s designed to be built one record at a time. This means you either:
      • Pass each record in your database fetch loop. (recommended for performance)
      • Loop through a pre-built array and pass each value. (example shown, not quite as efficient, decouples the classes nicely)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine you have a normal table view where each row is an item on
An example is shown below; imagine each commar separated date is a row in
Imagine I have a csv with and each value is an integer. so the
Imagine some kind of a banking application, with a screen to create accounts. Each
Is having a primary key that auto increments on each new row necessary? for
Imagine we have a set of entities each of which has its state: free,
I have a table with each row explaining something (an input command). In another
So imagine you have multiple tables in your database each with it's own structure
is there a way of getting table name for each column in a postgre
Imagine you homebrew a custom gui framework that doesn't use windows handles (compact framework,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.