开发者

PHP: problem inserting array inside an array

开发者 https://www.devze.com 2023-01-14 18:37 出处:网络
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:

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号