我使用以下代码从我的表中获取内容。但我需要稍微改变一下 JSON 的输出。
$rows = array();
if(isset($_GET['fruitName'])) {
$stmt = $pdo->prepare("SELECT variety FROM fruit WHERE name = ? ORDER BY variety");
$stmt->execute(array($_GET['fruitName']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($rows);
使用上面的代码,它显示如下:
[{"variety":"Apple"},{"variety":"Banana"},{"variety":"Orange"},{"variety":"Pear"}]
但我希望它是这样的:
[{"optionValue": "Apple", "optionDisplay": "Apple"}, {"optionValue": "Banana", "optionDisplay": "Banana"}, {"optionValue": "Orange", "optionDisplay": "Orange"}, {"optionValue": "Pear", "optionDisplay": "Pear"}]