我正在从表中获取几列并将其作为 json 返回,我想在将其发送回客户端之前向关联数组添加一个键。
我在 get 函数中执行此操作:
$stmt = $app->pdo->prepare("SELECT col1, col2, col3 FROM item_rnw");
$stmt->execute();
$rnws = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rnws as $rnw) {
$rnw["icon"] = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=•|000000";
}
echo json_encode($rnws);
然而,在客户端上记录响应仅显示 col1、col2、col3 键 + 值对,没有图标键 + 值。
PDO新手,谁能指出我做错了什么?
干杯
你也可以
这样做,保存循环:
$icon = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=•|000000";
$stmt = $app->pdo->prepare("SELECT col1, col2, col3, '$icon' as icon FROM item_rnw");
问题是您没有对数组中的项目进行操作。试试这个...
foreach($rnsw as &$rnw)