1

Could someone please tell me how to structure my php array so that i can plug it straight into the Google pie chart API? Here is my current code:

PHP:

// class containing sql

$browser_data = browser_data();

// array to populate titles
$data_array['titles'] = array('title', 'amount');



 //array to populate data
    foreach($browser_data as $k=>$val) {
        $data_array[$k] = array($k, $val['examples']);
    }

JS:

data_array = <?=json_encode($data_array)?>;
   var data = google.visualization.arrayToDataTable(data_array);

1 Answer 1

1

You need to remove 'titles' and '$k' from the array keys, as json_encode will create an object from an associative array instead of an array. This is what you need:

// array to populate titles
$data_array[] = array('title', 'amount');

// array to populate data
foreach($browser_data as $k=>$val) {
    $data_array[] = array($k, $val['examples']);
}

If your data source outputs numbers as strings (some databases, including MySQL, do this), you need to add JSON_NUMERIC_CHECK to the json_encode call:

data_array = <?=json_encode($data_array, JSON_NUMERIC_CHECK)?>;
Sign up to request clarification or add additional context in comments.

Comments

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.