PHP nested array from flat MySQL query result

kabauter

I need to create a line chart using highcharts.com js. The plugin requires JSON data with the following structure:

series: [{
    {
    name: 'book 2',
    data: [
            1970, 120,
            2001,  50,
            2005, 180,
            2014,  50
          ]
    }, 
    {
    name: 'another book',
    data: [
            1970, 120,
            2001,  50,
            2005, 180,
            2014,  50
          ]
    }
            }]

(the data is just an example)

I want to query the needed data from a MySQL Database. The data is extracted with the meekrodb.com library in PHP.

$results = DB::query("SELECT booktitle, EditionNr, Year FROM editions");

The query so far outputs this flat array:

    (
        [0] => Array
            (
                [booktitle] => booktitle_a
                [EditionNr] => 11
                [Year] => 2012
            )
        [1] => Array
            (
                [booktitle] => booktitle_a (the same)
                [EditionNr] => 12
                [Year] => 2013
            )

        [2] => Array
            (
                [booktitle] => another_booktitle
                [EditionNr] => 1
                [Year] => 2000
            )
...

The top level indexes correspond to the rows of the result of the query. However the data output must be hierarchial. How can I convert it to a nested array that looks like this?

Array
(
    [name] => book_title_a
    [data] => Array
        (
            [0] => 2012, 11  // these are the rows Year (=2012) and EditionNr (=11th edition)
            [1] => 2013, 12
        )

    [name] => another_book_title
    [data] => Array
        (
            [0] => 2000, 1
            [1] => 2011, 2
            [2] => 2012, 3
        )
)

I appreciate your help.

-Andi

Phil

Try something like this:

$data = array();

foreach ( $rows as $row ) {
  $bookTitle = $row['booktitle'];
  if ( !isset( $data[$bookTitle] ) ) {
    $data[$bookTitle] = array( "name" => $bookTitle, "data" => array() );
  }
  $data[$bookTitle]['data'][] = array( $row['Year'], $row['EditionNr'] );
}

echo json_encode( array_values( $data ) );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

php array from result of mysql query

From Dev

Creating a nested UL from flat array in PHP

From Dev

How to query one result of genre from array genres in php and mysql?

From Dev

Save mysql query result into a PHP array

From Dev

Storing MySQL query result into a PHP array

From Dev

convert a 'flat array' into 'nested array' with cakephp(or php)

From Dev

PHP not displaying result from MYSQL query

From Dev

Multiple nested array from MySQL query

From Dev

Generating a flat array from nested for loops in coffeescript

From Dev

How to make nested JSON result from MySQL query in NodeJS?

From Dev

Make a flat array from Elasticsearch query results

From Dev

Make a flat array from Elasticsearch query results

From Dev

Having Trouble Getting a MySQL Query Result into a PHP Array

From Dev

Php/MYSQl to output first and last result of query foreach value of an array?

From Dev

MySQL query from multiple tables with an array (PHP)

From Dev

MySQL query from multiple tables with an array (PHP)

From Dev

Cumulative sum in PHP Array from MySQL Query

From Dev

Flat array to nested by keys

From Dev

Mysql like query result using lodash from json array

From Dev

Skip loop to populate form if result array from mysql query is empty

From Dev

How to create array of nested comments out of flat array from DB

From Dev

Javascript - dynamically generate an infinitely nested Array from a flat Array

From Dev

How to display multiple result from MySql query using PHP

From Dev

How to display multiple result from MySql query using PHP error

From Dev

Query from php and in Mysql Workbench not same result for special char

From Dev

Loop array in mysql query and for the result

From Dev

PHP postgresql: INSERT values from query array result

From Dev

Pass result of SQL query from PHP to Javascript array

From Dev

PHP mysql query result to xml

Related Related

  1. 1

    php array from result of mysql query

  2. 2

    Creating a nested UL from flat array in PHP

  3. 3

    How to query one result of genre from array genres in php and mysql?

  4. 4

    Save mysql query result into a PHP array

  5. 5

    Storing MySQL query result into a PHP array

  6. 6

    convert a 'flat array' into 'nested array' with cakephp(or php)

  7. 7

    PHP not displaying result from MYSQL query

  8. 8

    Multiple nested array from MySQL query

  9. 9

    Generating a flat array from nested for loops in coffeescript

  10. 10

    How to make nested JSON result from MySQL query in NodeJS?

  11. 11

    Make a flat array from Elasticsearch query results

  12. 12

    Make a flat array from Elasticsearch query results

  13. 13

    Having Trouble Getting a MySQL Query Result into a PHP Array

  14. 14

    Php/MYSQl to output first and last result of query foreach value of an array?

  15. 15

    MySQL query from multiple tables with an array (PHP)

  16. 16

    MySQL query from multiple tables with an array (PHP)

  17. 17

    Cumulative sum in PHP Array from MySQL Query

  18. 18

    Flat array to nested by keys

  19. 19

    Mysql like query result using lodash from json array

  20. 20

    Skip loop to populate form if result array from mysql query is empty

  21. 21

    How to create array of nested comments out of flat array from DB

  22. 22

    Javascript - dynamically generate an infinitely nested Array from a flat Array

  23. 23

    How to display multiple result from MySql query using PHP

  24. 24

    How to display multiple result from MySql query using PHP error

  25. 25

    Query from php and in Mysql Workbench not same result for special char

  26. 26

    Loop array in mysql query and for the result

  27. 27

    PHP postgresql: INSERT values from query array result

  28. 28

    Pass result of SQL query from PHP to Javascript array

  29. 29

    PHP mysql query result to xml

HotTag

Archive