0

I am using http://www.jqplot.com/tests/pie-donut-charts.php which needs to have data given to it in the following format:

//JavaScript
var data = [
  ['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14],
  ['Out of home', 16],['Commuting', 7], ['Orientation', 9]
];

The data is generated serverside with PHP. I am manually creating the string as follows:

//PHP
$string.='["'.$row['name'].'",'.$row['count'].'],';

I would rather just create an array, and use json_encode() or something similar to create the data. Any suggestions how I would do so?

2
  • How about json_encode($array);? Commented Aug 22, 2012 at 15:37
  • It didn't work. I think because it would encode it as an object with {} Commented Aug 22, 2012 at 15:41

1 Answer 1

1
$array = array(
    array(
        "Heavy Industry",
        12
    ),
    array(
        "Retail",
        9
    )
);
$json = json_encode($array);
var_dump($json); // "[["Heavy Industry",12],["Retail",9]]"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Arxanas. Maybe it does work :). Let me check it out and make sure.
I get different results: echo manual created string ["Heavy Industry",44],["Retail",24] echo print_r($array,1) Array ( [Heavy Industry] => 44 [Retail] => 24 ) echo var_dump($array) array(7) { ["Heavy Industry"]=> string(2) "44" ["Retail"]=> string(2) "24" }
But when I run your exact script, I get correct results. Let me investigate. Thanks
Opps, I was doing $array=array("Heavy Industry"=>12, "Retail"=>9); Thanks for your help!

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.