替换数组键

时间:2013-10-06 23:31:46

标签: php arrays

我有一个这样的数组JSON Example。我正在尝试替换数字键......“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);    
    }

0 个答案:

没有答案