【发布时间】:2017-02-22 11:29:49
【问题描述】:
我有问题,我不确定什么是最好的处理方法,因为我没有太多的 PHP 经验。
所以我有从数据库返回所有项目的基本功能,它看起来像这样。
function getAll($mysqli) {
$stmt = $mysqli->prepare("SELECT g.id, g.name, g.description FROM control_group as g");
$stmt->execute();
$stmt->bind_result($id, $name, $description);
$groups = array();
while ($stmt->fetch()) {
$groups[$id] = array(
'id' => $id,
'name' => $name,
'description' => $description
);
}
$stmt->close();
$mysqli->close();;
echo json_encode($groups);
}
那个函数会像这样重现响应
5:{id: 5, name: "Group 1", description: null}
11:{id: 11, name: "Group 2", description: null}
14:{id: 14, name: "Group 3", description: null}
17:{id: 17, name: "Group 4", description: null}
我想在这里实现的是这样的响应
0:{id: 5, name: "Group 1", description: null}
1:{id: 11, name: "Group 2", description: null}
2:{id: 14, name: "Group 3", description: null}
3:{id: 17, name: "Group 4", description: null}
所以我想显示正确的索引而不是项目 id,我知道我需要检查索引而不是 id,但我不知道如何正确地做到这一点。
$groups[index] = array(
'id' => $id,
'name' => $name,
'description' => $description
);
【问题讨论】:
标签: php