[TIP] PHP Function to convert SQL arrays to DataTables aaData json
[TIP] PHP Function to convert SQL arrays to DataTables aaData json
Maybe this could help people to save few lines of code, feel free to improve
Works on Oracle 11g, but im almost sure it would work with any array made with _fetch_all();
[code]
function Array2aaData($array) {
$out = "{ \"aaData\": [ ";
$arrKeys = array_keys($array);
$arrVals = array_values($array);
for ($x = 0; $x <= count($arrVals[0]) - 1; $x++) {
$out .= "[";
for ($i = 0; $i <= count($arrKeys) - 1; $i++) {
$out .= "\"" . addslashes($array[$arrKeys[$i]][$x]) . "\"";
if (!($i == count($arrKeys) - 1)) {
$out .= ",";
}
}
if (!($i == count($arrVals[0]) - 1)) {
$out .= "],";
}
}
$out = substr($out, 0, strlen($out) - 1);
$out .= "] }";
return $out;
}
[/code]
Works on Oracle 11g, but im almost sure it would work with any array made with _fetch_all();
[code]
function Array2aaData($array) {
$out = "{ \"aaData\": [ ";
$arrKeys = array_keys($array);
$arrVals = array_values($array);
for ($x = 0; $x <= count($arrVals[0]) - 1; $x++) {
$out .= "[";
for ($i = 0; $i <= count($arrKeys) - 1; $i++) {
$out .= "\"" . addslashes($array[$arrKeys[$i]][$x]) . "\"";
if (!($i == count($arrKeys) - 1)) {
$out .= ",";
}
}
if (!($i == count($arrVals[0]) - 1)) {
$out .= "],";
}
}
$out = substr($out, 0, strlen($out) - 1);
$out .= "] }";
return $out;
}
[/code]
This discussion has been closed.