大家好,我有 microsoft sql management studio 18,其中有一个数据库。我正在通过 php 执行这样的 select 语句: $conn = OpenCon(); $query = "SELECT id, name, picture, description, numberOfEngines FROM planes"; $result = sqlsrv_query($conn, $query); if ($result === false) { $status['status'] = "0"; echo json_encode($status); } else{ while($row = sqlsrv_fetch_array($result)) { $theRows[] = $row; } echo json_encode($theRows); } CloseCon($conn);这是输出:[{"0":1,"id":1,"1":"114","name":"114","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},{"0":2,"id":2,"1":"115","name":"115","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},{"0":3,"id":3,"1":"124","name":"124","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1},{"0":4,"id":4,"1":"125","name":"125","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1}]我怎样才能删除那些出现两次的重复项,例如“0”:1或ariplane.png。所以我的输出将是这样的:[{"id":1, "name":"114", "picture":"airplane1.png", "description":"Cessna C525", "numberOfEngines":1}]
1 回答
慕莱坞森
TA贡献1810条经验 获得超4个赞
要仅返回数组中的关联键,请将参数SQLSRV_FETCH_ASSOC
作为fetchType
参数传递给sqlsrv_fetch_array
:
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
这将为您提供一个包含以下条目的数组:
{"id":1,"name":"114","picture":"airplane1.png","description":"Cessna C525","numberOfEngines":1}
如果您确实想要其他数字键,请保持代码不变并添加
unset($row[0])
前
$theRows[] = $row;
- 1 回答
- 0 关注
- 106 浏览
添加回答
举报
0/150
提交
取消