tags:

views:

45

answers:

2

Hi folks,

I have a script that is using the google charts API and the gChart wrapper.

I have an array that when dumped looks like this:

$values = implode(',', array_values($backup));
var_dump($values);
string(12) "8526,567,833"

I want to use the array like this:

$piChart = new gPieChart();
$piChart->addDataSet(array($values));

I would have thought this would have looked like this:

 $piChart->addDataSet(array(8526,567,833));

Howerver when I run the code it creates a chart with only the first value.

Now when I hardcode the values in instead I get each value in the chart.

Does anyone know why it's acting this way?

Jonesy

+5  A: 

I think

$piChart->addDataSet(array_values($backup));
// or just: $piChart->addDataSet($backup); depends on $backup

should do it.

$values only contains a string. So if you do array($values), you create an array with one element:

$values = "8526,567,833";
print_r(array($values));

gives

Array
(
    [0] => 8526,567,833
)

array(8526,567,833) would be the same as array_values($backup) or maybe even just $backup, that depends on the $backup array.

Felix Kling
+1 for being the right answer.
hopeseekr
yep that did it, thanks a lot!
iamjonesy
+3  A: 

Looks like you want to use $backup instead of $values as $values is the imploded string... and since 8526,567,833 isn't a valid number, it parses 8526 and leaves the rest alone.

Jeff Rupert