I'm working on an application where the user should see a list with charts. The data for the charts should be fetched from a database (which currently counts about 785 rows) and then get sorted to form a valid JSON string. Right not I try to do it like this
while($row = $res->fetch_assoc()) {
if(count($appData) == 0 ){
$appData[] = array(
"name" => $row["name"],
"date" => array($row["date"]),
"android" => array($row["android_count"]),
"ios" => array($row["apple_count"])
);
}else {
for($i = 0; $i < count($appData); $i++) {
if($appData[$i]["name"] == $row["name"]){
$appData[$i]["date"][] = $row["date"];
$appData[$i]["android"][] = $row["android_count"];
$appData[$i]["ios"][] = $row["apple_count"];
}else {
$appData[] = array(
"name" => $row["name"],
"date" => array($row["date"]),
"android" => array($row["android_count"]),
"ios" => array($row["apple_count"])
);
}
}
}
}
echo json_encode($appData);
When I try to run the code it will give an "Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 71 bytes)" error. I've tried to increase the maximum allowed memory, just to see what would happen, but I got the same result.
Is there any way to avoid having to do so many loops? Or should I approach this from a completely different way, and if so which one?
The end result should look something like this
[{"name":"Some name", "date":["2016-05-09", "2016-05-10", "2016-05-11"], "android":["3", "1", "8"], "ios":["4", "7", "5"]},...]
All help would be appreciated!