PHP JSON 在 MySQL 中编码数值数组

PHP JSON encode numerical array in MySQL

我尝试在 Mapbox GL Draw 中保存和编辑线条,为此我使用 MySQL 和 PHP(要求),并且只在 MySQL 中保存坐标,但我做了在 JSON 中查询和编码,在 return 中编程 this

"geometry": {
    "coordinates": "[[8.971375670640924,39.27952971962691],[9.019097533433808,39.29998983691581],[9.062012877672174,39.27554328682473],[9.125870909898737,39.28643899912291],[9.14097711107047,39.30742839737994],[9.178742614000214,39.30344355258791]]",
    "type": "LineString"
}

我需要数值数组中的坐标,这

"geometry": {
    "coordinates": [
        [
            9.06905,
            39.248296
        ],
        [
            9.111966,
            39.26212
        ],
        [
            9.135998,
            39.239256
        ],
        [
            9.039472,
            39.25596
        ],
        [
            9.042251,
            39.25373
        ]
    ],
    "type": "LineString"
}

它在 MySQL 中的坐标如何 JSON(长文本 utf8mb4_bin),查询 1 个数据是

while($row = mysqli_fetch_assoc($result_task)) {
    $ruta['features'][0]['geometry']['coordinates'] = $row['ruta'];
}

根据您显示的数据,数据库中的 ruta 已经是 JSON。在分配给数组之前对其进行解码然后对其进行编码:

while($row = mysqli_fetch_assoc($result_task)) {        
    $ruta['features'][0]['geometry']['coordinates'] = json_decode($row['ruta']);
}
$result = json_encode($ruta);

在这个例子中不需要循环,只是 运行 mysqli_fetch_assoc.