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 "]";
?>