我有以下代码来获取问题时间和答案的所有详细信息:
<?php
session_start();
include('education/hub/kautilya/connection.php');
function getAllAnswersAndTime($papId)
{
$c=new Connection;
$conn=$c->getConnection();
$date=date("Y-m-d");
$sql="select GROUP_CONCAT(`all_time_questions`) as `time`, GROUP_CONCAT(`all_answers`) as `answers` from `result` where `paper_id` = '$papId'";
$record=$conn->query($sql);
return $record;
}
$papId = $_REQUEST['papId'];
$resall = getAllAnswersAndTime($papId);
$alltime = "";
$allans = "";
function multiexplode ($delimiters,$data) {
$MakeReady = str_replace($delimiters, $delimiters[0], $data);
$Return = explode($delimiters[0], $MakeReady);
return $Return;
}
$totalCandidates = 0;
foreach($resall as $row)
{
$totalCandidates +=1 ;
$alltime = $row['time'];
$allans = $row['answers'];
}
unset($resall);
$alltime = ltrim($alltime, "::");
$all_time = multiexplode(array("::","=>"), $alltime);
$all_ans = multiexplode(array("::","=>"), $allans);
$total = 0;
$total_times = [];
$total_ans = [];
for($i = 0; $i < count($all_time); $i+=1)
{
if($i%2==1)
{
$total_times[$all_time[$i-1]] += $all_time[$i];
$total_ans[$all_time[$i-1]] = 0;
}
}
for($i = 0; $i < count($total_times); $i+=1)
{
$total_times[$i] /= $totalCandidates;
}
for($i = 0; $i < count($all_ans); $i+=1)
{
if($i%2==1)
{
$total_ans[$all_ans[$i-1]] += 1;
}
}
for($i = 0; $i < count($total_ans); $i+=1)
{
$total_ans[$i] /= $totalCandidates;
$total_ans[$i] *= 100;
}
$cc['tot_ans'] = $total_ans;
$cc['tot_time'] = $total_times;
echo json_encode($cc);
?>
我在error_log中收到一个“允许的内存大小”错误。我已经阅读了其他问题,并尝试设置memory_limit=2048M
并优化了查询,以使其返回串联的字符串,但似乎无法解决问题。有人可以告诉我如何解决这个问题吗?
数据也不是很大,数组只有两行,大约有20个条目。