Search code examples
phparraysgchart

PHP: problem inserting array inside an array


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


Solution

  • 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.