gpt4 book ai didi

php - 从mysql结果创建数组?

转载 作者:行者123 更新时间:2023-11-29 04:39:58 24 4
gpt4 key购买 nike

我正在尝试创建一个数组();来自 mysql 结果/查询。

Array OUTPUT 应该看起来完全像这样:

{"1":{"info":"Rooz","lat":51.503363,"lng":-0.127625},
"2":{"info":"Michelle","lat":51.503343,"lng":-0.127567}}

所以我试着这样做:

$sql = "SELECT id, name, lat, lng FROM positions ORDER BY id";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
$testLocs = array();
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){

$testLocs[] = $row;

}
print_r(json_encode($testLocs));

但是上面代码的输出是这样的:

[{"id":"1","name":"Rooz","lat":"51.5033630","lng":"-0.1276250"},{"id":"2","name":"Michelle","lat":"51.503343","lng":"-0.127567"}]

我不知道如何在数组中实现我想要的输出。

有人可以就此提出建议吗?

如有任何帮助,我们将不胜感激。

最佳答案

改变

$testLocs[] = $row;

$testLocs[$row["id"]] = array("info"=>$row["name"], "lat"=>$row["lat"], "lng"=>$row["lng"]);

关于php - 从mysql结果创建数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31818340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com