I use the following code to get content from my table.
But I need to change the output of the JSON a little bit.
$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);
With the above code it show it like this:
[{"variety":"Apple"},{"variety":"Banana"},{"variety":"Orange"},{"variety":"Pear"}]
But I want it to be like this:
[{"optionValue": "Apple", "optionDisplay": "Apple"}, {"optionValue": "Banana", "optionDisplay": "Banana"}, {"optionValue": "Orange", "optionDisplay": "Orange"}, {"optionValue": "Pear", "optionDisplay": "Pear"}]
One solution could be to adjust the SQL-query like this:
Another approach would be to parse each row individually and build up your array by hand. So instead of
you could use