简体   繁体   中英

how can I adapt my array data to google chart matrix style array in PHP?

In my application, I need to build an array which I send to my view to build a google chart.

The final ouput should look like this :

data = google.visualization.arrayToDataTable([
    ['Year', 'Sales', 'Expenses'],
    [2004,  null,      400],
    [2005,  1170,      null],
    [2006,  660,       1120],
    [2023,  1030,      540]
]);

The initial data I have is :

$sales = array(2005=>1170, 2006=>660, 2023=>1030);
$expenses = &array(2004=>400, 2006=>1120, 2023=>540);

How can I adapt this data to the required format ? Of course I need this to be va&lid for any dimension.

I'm a bit lost here cause everything is values in the destination format and I need to populate null values for the years that are not included in my source data.

How can I solve this ?

You can make 3 arrays or make an array, with 3 indexes, of arrays.

<?php

$data = array();
$data['Year'] = array(2004, 2005, 2006, 2023);
$data['Sales'] = array(1000, 1170, 660, 1030);
$data['Expenses'] = array(400, null, 1120, 540);

?>

You can then iterate each row or a column as you need. You can also populate or read out using a FOR loop:

<?php
echo "data = google.visualization.arrayToDataTable([\r\n";
echo "['Year', 'Sales', 'Expenses'],\r\n";

for($i=0;$i<count($data['Year'];$i++)){
  echo "[" . $data['Year'][$i] . ", " . $data['Sales'][$i] . ", " . $data['Expenses'] . "],\r\n";
}

echo "]";
?>

If your values are more dynamic, meaning you might not get the same number of indexes in each array, just take a count of each, and set the limiter to that length in your loop. You can then use empty() , to check if the value is there, and replace with null as needed.

Update:

With this type of data set:

$sales = array(2005=>1170, 2006=>660, 2023=>1030);
$expenses = &array(2004=>400, 2006=>1120, 2023=>540);

You have a difficult time aligning the indexes. For example 2005 in one, and 2004 in another, but not in both. I think you need to walk each one, and build a list of indexes from both. Resulting in: 2004, 2005, 2006, 2023 .

<?php
$indexes = array();
foreach($sales as $k => $v){
  $indexes[] = $k;
}
foreach($expenses as $k => $v){
  $indexes[] = $k;
}

sort($indexes);
echo "data = google.visualization.arrayToDataTable([\r\n";
echo "['Year', 'Sales', 'Expenses'],\r\n";
foreach($indexes as $in){
  if(empty($sales[$in])){
    echo "[$in, null, {$expenses[$in]}],\r\n";
  } elseif(empty($expenses[$in])) {
    echo "[$in, {$sales[$in]}, null,\r\n";
  } else {
    echo "[$in, {$sales[$in]}, {$sExpenses[$in]}],\r\n";
  }
}
echo "]";
?>

Why not have an array of arrays and output that? Something like below should work

$visualizationDataTable = [
    ['Year', 'Sales', 'Expenses'],
    [2004, 1000, 400],
    [2005, 1170, null],
    [2006, 660, 1120],
    [2023, 1030, 540]
];

echo "data = google.visualization.arrayToDataTable([";
foreach($visualizationDataTable as $visualizationDataTableRow) {
    echo "[";
    foreach($visualizationDataTableRow as $visualizationDataTableColumn) {
        if(is_string($visualizationDataTableColumn)) {
            echo "'$visualizationDataTableColumn'";
        } else if(is_numeric($visualizationDataTableColumn)) {
            echo "$visualizationDataTableColumn";
        } else if(is_null($visualizationDataTableColumn)) {
            echo "null";
        }

        echo ",";
    }
    echo "],";
}
echo "]);";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM