1

我有一个像这样的JSON 示例的数组。我正在尝试用字符串键替换数字键 ..."conn":{" 1 ":{"... ,例如 "node"。

例如,我想创建这个:

{
"Level": [
    {
        "main": "472321514",
        "main_lat": "39.1057579",
        "main_lon": "26.5451331",
        "conn": {
            "node": {
                "id": "599416249",
                "coords": {
                    "lat": "39.1055889",
                    "lon": "26.5452403"
                },
                "distance": 0.0209442235276
            },...

在 json 编码之前,我的脚本是:

foreach ($ways as $w){
        $nd=$w->nd;
        foreach ($nd as $w2){

                $nodes_Array[]=(string)$w2->attributes()->ref;
            }
            for($ww=0;$ww<count($nodes_Array);$ww++){
                $nodes_Array2[$bb]['main'] = $nodes_Array[$ww];
                for($gg=0;$gg<count($node_content);$gg++){
                    if($node_content[$gg]['id']==$nodes_Array2[$bb]['main']){ 
                    $nodes_Array2[$bb]['main_lat']= $node_content[$gg]['lat'];
                    $nodes_Array2[$bb]['main_lon']= $node_content[$gg]['lon'];
                    }
                }
                $nodes_Array2[$bb]['conn'] = array_diff($nodes_Array, array($nodes_Array[$ww]));    
                for($cc=0;$cc<count($nodes_Array2[$bb]['conn']);$cc++){
                    for($gg=0;$gg<count($node_content);$gg++){
                        if($node_content[$gg]['id']==$nodes_Array2[$bb]['conn'][$cc]){ 
                    $nodes_Array2[$bb]['conn'][$cc]=Array( 
                         'id'=>$node_content[$gg]['id'], 
                         'coords'=>Array( 
                             'lat'=>$node_content[$gg]['lat'], 
                             'lon'=>$node_content[$gg]['lon'], 
                             ),
                         'distance'=>distance($nodes_Array2[$bb]['main_lat'],$nodes_Array2[$bb]['main_lon'],$node_content[$gg]['lat'],$node_content[$gg]['lon'],"K"), 
                         );
                        }
                    }
                }
                $bb++;
            }   
            unset($nodes_Array);    
    }
4

0 回答 0