-
-
Save pkvip9999/c097f539316f27d32c0565aeeb1f37b5 to your computer and use it in GitHub Desktop.
A "simple" PHP function to convert arrays into DataTable JSON export format fit to use with Google Charts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* arrayToDataTable() | |
* Converts PHP arrays to nested arrays fit for json_encode() | |
* | |
* Format Notice: | |
* - $cols contains all the columns for the DataTable, each column having a type and a label. | |
* Columns can be defined as plain arrays (containing only type and label, see below) or as a named array, | |
* with each of the properties defined as key => value pairs. All columns must have at least a 'type'. | |
* | |
* - $rows contain the data for the DataTable, each row contained in an array. Similar approach works for $rows as | |
* did for $cols, where values can be defined either as plain values inside the row or as a named array. | |
* Every row must contain the same amount of values (cells) as there are columns, null values are accepted. | |
* | |
* - This implementation attempts to follow Google's DataTable JSON format as closely as possible. | |
* | |
* - Example $cols: | |
* $cols = array( | |
* array('type' => 'string', 'label' => 'Time'), 'id' => 'time', // Named Array | |
* array('number', 'Hits'), // Plain Array | |
* ); | |
* | |
* - Example $rows: | |
* $rows = array( | |
* array( | |
* '2013-06-28', // Plain Value | |
* array('v' => 7, 'f' => 'Seven', 'p' => array('style' => 'border: 1px solid green;')), // Value as a Named Array | |
* ), | |
* ); | |
* | |
* @see https://developers.google.com/chart/interactive/docs/reference#dataparam | |
* @author Diftraku <diftraku(at)derpy(dot)me> | |
* @param array $cols | |
* @param array $rows | |
* @return null | |
* @throws IllegalArgumentException | |
*/ | |
static function arrayToDataTable(array $cols, array $rows) { | |
if (!empty($rows) && !empty($cols)) { | |
if (count($cols) !== count($rows[0])) { | |
// Values specified in the first row of $rows don't add up to the ones in $cols | |
throw new IllegalArgumentException('Amount of values in rows must match amount of columns'); | |
} | |
$return = array(); | |
// Process columns | |
foreach ($cols as $column) { | |
// Check if the column has a type | |
if (!array_key_exists('type', $column) && array_key_exists(0, $column)) { | |
// Try to mangle us a type | |
switch ($column[0]) { | |
case 'boolean': | |
case 'number': | |
case 'string': | |
case 'date': | |
case 'datetime': | |
case 'timeofday': | |
$column['type'] = $column[0]; | |
break; | |
default: | |
$column['type'] = 'string'; | |
break; | |
} | |
unset($column[0]); | |
// Assume the second entry is a label | |
if (array_key_exists(1, $column)) { | |
$column['label'] = $column[1]; | |
unset($column[1]); | |
} | |
} | |
// @TODO: Add key => val validation here? | |
$return['cols'][] = $column; | |
} | |
// Process rows | |
foreach ($rows as $row) { | |
// <3 Nested Arrays | |
$tmp = array('c' => array()); | |
foreach ($row as $key => $cell) { | |
// Is our cell a plain or an array with other stuff? | |
if (!is_array($cell)) { | |
$cell = array('v' => $cell); | |
} | |
// @TODO: Add key => val validation here? | |
$tmp['c'][] = $cell; | |
} | |
$return['rows'][] = $tmp; | |
} | |
return $return; | |
} | |
else { | |
// We can't work without something to work on | |
throw new IllegalArgumentException('Unable to process empty arrays'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment