因此,我试图在数组中对象(?)的末尾插入一个元素(?)。
foreach ($results as $key => $result) {
// pak orginele vraag + titel
$sql = "SELECT * FROM mdl_evaluation_question WHERE id = ".$key;
$question = $conn->query($sql) or die($conn->error);
while($q = $question->fetch_assoc()) {
// check of vraag van toepassen is voor de docent & bereken gemiddelde
if ($q['teacher'] == 1) {
array_push($info, array('title' => $q['title'],
'Gemiddelde' => number_format($result['result']/$result['times'], 1)));
// check if there is any feedback
if ($result['feedback'] > 0) {
// So here I'm trying to add the feedback behind the 'gemiddelde'
array_push($info, array('feedback' => 'feedbacks.php?course='.$_GET['course'].'&date='.$_GET['date'].'&question='.$key.'">'.$result['feedback'].' feedback(s) gegeven'));
}
}
}
}
echo json_encode($info);
所以我得到的是:
[{"title":"Inschrijven voor de cursus","Gemiddelde":"7.4"}]
但是我要尝试的是,如果有任何反馈,请在方括号内的“ gemiddelde”后面添加反馈。例如:
[{"title":"Inschrijven voor de cursus","Gemiddelde":"7.4", "feedback": "something"}]
答案 0 :(得分:0)
只需在数组名称后使用[]添加反馈信息。
// check if there is any feedback
if ($result['feedback'] > 0) {
$info[] = $result['feedback'];
}
答案 1 :(得分:0)
如果我有问题,这应该可以工作。 如果存在反馈,则要添加到当前对象,对吗?
foreach ($results as $key => $result) {
// pak orginele vraag + titel
$sql = "SELECT * FROM mdl_evaluation_question WHERE id = ".$key;
$question = $conn->query($sql) or die($conn->error);
while($q = $question->fetch_assoc()) {
if ($q['teacher'] == 1) {
// Initiliaze object
$curArray = array(
'title' => $q['title'],
'Gemiddelde' => number_format($result['result']/$result['times'], 1)
);
// check if there is any feedback
if ($result['feedback'] > 0) {
// Add feedback to current object
$curArray['feedback'] = 'feedbacks.php?course='.$_GET['course'].'&date='.$_GET['date'].'&question='.$key.'">'.$result['feedback'].' feedback(s) gegeven';
}
// Add the object to the array
array_push($info, $curArray);
}
}
}
echo json_encode($info);