1

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

1
  • in my earlier example I messed up with the [] brackets an nested the array too deeply. Corrected it now Commented Apr 6, 2014 at 14:28

3 Answers 3

1

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 ) );
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. It seems to work now. However I changed one line so that the keys inside the "data" array are not nested too deeply: $data[$bookTitle]['data'][] = $row['Year'].", ".$row['EditionNr'];
@vp_arth: you're right, but doesn't $data[$bookTitle]['data'][] = array( $row['Year'], $row['EditionNr'] ); create an array inside an array (because of [] AND array())? thus nesting it one branch too deeply.
no, this exactly what are you ask, data is array, and adds [Year, Nr] pairs to this array here
if you have a number entities with same bookTitle it will be data: [[Year1, Nr1], [Year2, Nr2]...]
0
$result = array();
$prevbooktitle = '';
$book_count = 0;
foreach($tmp as $val){

    if($val['booktitle'] != $prevbooktitle){
      $result[$book_count]['name'] = $val['booktitle'];
      $book_count++;
      $prevbooktitle = $val['booktitle'] ;
    }

    $result[$book_count]['data'][] = array($val['Year'],$val['EditionNr']);

}

Comments

0
    $series = new stdClass();
    $JSONBooksArray = array();
    foreach($books AS $book){

        $jsonbook = new stdClass();
        $jsonbook->name = $book['booktitle'];
        $bookdata = array();
        $bookdata[0] = $book['Year'];
        $bookdata[1] = $book['EditionNr'];
        $jsonbook->data = $bookdata;
        $JSONBooksArray[] = $jsonbook;

    }
    $series->series = $JSONBooksArray;
    echo json_encode($series,JSON_PRETTY_PRINT);

1 Comment

It's a very readable approach using classes, but it generates duplicates for the booktitle entries instead of grouping the same book.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.