I am working with some PHP code right now, trying to return JSON to calling pages. The problem I am running into is that one of the arrays I am using is returning the array indexes as key values. The code I am using is roughly as follows:
Code:
$data = pg_query($query) or die('Data query failed: ' . pg_last_error());
$curDate = new DateTime('1970-01-01 00:00:00');
$dataPoint = 0;
while($row = pg_fetch_row($data)) {
$newDate = new DateTime($row[0] . " -" . $offset . ' hours');
if($curDate != $newDate) {
$dataPoint++;
$curDate = $newDate;
$output['catalog'][$dataPoint] = array();
$output['catalog'][$dataPoint]['dt'] = $curDate->format('Y-m-d H:i:s');
$output['catalog'][$dataPoint]['val'] = array();
foreach ($ids as $i => $id) {
$output['catalog'][$dataPoint]['val'][$i] = null;
}
}
foreach($ids as $i => $id) {
if($id == $row[1]) {
$output['catalog'][$dataPoint]['val'][$i] = number_format(((((intval($row[2]) * $conversion[$i][0]) + $conversion[$i][1]) * $conversion[$i][2]) + $conversion[$i][3]), $conversion[$i][4], '.', '');
break;
}
}
}
echo json_encode($output);
(The query pulls in a very large amount of data, one row per variable sought, many of which are read at the same time, so the code is meant to loop through and combine all concurrent data points into one array, then put that array in an array of data polling times)
The problem is that the JSON I receive has the format below:
Code:
{"catalog":{"1":{"dt":"2009-07-16 20:02:33","val":[...,...,...]},"2":{"dt":"2009-07-16 20:03:17","val":[...,...,...]},"3":{"dt":"2009-07-16 20:04:17","val":[...,...,...]}
Anyone know why this is happening? I need to not show the indexes of the objects which contain 'dt' and the array 'val' (here '1', '2', and '3') in order to comply with the expected data format, but I have no idea how to do this. Thanks for any help you can offer!