动态更改我的数组

时间:2016-06-01 19:54:47

标签: php json web-services

我有一个返回如下数组的函数:

[
"var0 =>    300  PR. D. JOÃO",
"var1 =>a passar",
"var2 =>",
"var3 =>    904  Q.BELA VISTA",
"var4 =>a passar",
"var5 =>",
"var6 =>    905  MONTE VIRGEM",
"var7 =>a passar",
"var8 =>",
"var9 =>    300  TELHEIRA - C",
"var10 =>a passar",
"var11 =>",
"var12 =>   200  CAST. QUEIJO",
"var13 =>20:39",
"var14 =>5min"

]

我需要下一个例子的结果: 这种格式是必要的。

[
"route" =>  "300  PR. D. JOÃO",
"eta" =>"a passar",
"waittime" =>"",
"route" =>  "904  Q.BELA VISTA",
"eta" =>"a passar",
"waittime" =>"",
"route" =>" 905  MONTE VIRGEM",
"eta" =>"a passar",
"waittime" =>"",
"route" =>" 300  TELHEIRA - C",
"eta" =>"a passar",
"waittime" =>"",
"route" =>" 200  CAST. QUEIJO",
"eta" =>"20:39",
"waittime" =>"5min"
]

我的webservice PHP代码是:

function getPage ($codigo) {
    $link = "http://www.stcp.pt/pt//itinerarium/soapclient.php?codigo=";
    $dom = new DOMDocument();
    $homepage = file_get_contents($link.$codigo);
    $dom->loadHTML($homepage);
    $tables = $dom->getElementsByTagName('table');
    $table = $tables->item(0);
    $i = 0;
    $var = array();
    foreach ($table->childNodes as $tr) {
      if ($tr->nodeName == 'tr') {
        foreach ($tr->childNodes as $td) {
            $i++;
            $tds= $td->nodeValue;
            if ($i%2 == 0) {
            } else {
                array_push($var, $tds);                 
            }
        }
      } else {
        echo "string ";
      }
    }
    $first = array_shift($var);             
    $second = array_shift($var);                
    $third = array_shift($var);
    $maxvar = sizeof($var);
    $final_array = array();
    for ($k=0; $k < sizeof($var); $k++) { 
        ${'var' . $k} = $var[$k];
        array_push($final_array, "var$k =>".$var[$k]);
    }
    echo json_encode($final_array);
}

如何像我展示的例子一样返回json?

1 个答案:

答案 0 :(得分:1)

由于关联数组必须具有唯一键,因此输出中不能出现routeetawaittime多次。但是你可以创建一个非常好地表示数据的嵌套数组。

$a = [
"var0 =>    300  PR. D. JOÃO",
"var1 =>a passar",
"var2 =>",
"var3 =>    904  Q.BELA VISTA",
"var4 =>a passar",
"var5 =>",
"var6 =>    905  MONTE VIRGEM",
"var7 =>a passar",
"var8 =>",
"var9 =>    300  TELHEIRA - C",
"var10 =>a passar",
"var11 =>",
"var12 =>   200  CAST. QUEIJO",
"var13 =>20:39",
"var14 =>5min"
];

function removeVarPrefix($x) {
    return preg_replace('/^var\d+\s*=>\s*/', '', $x);
}

$b = array_map(function($set) {
    list($route, $eta, $waittime) = array_map('removeVarPrefix', $set);
    return ['route'=>$route, 'eta'=>$eta, 'waittime'=>$waittime];
}, array_chunk($a, 3));

echo json_encode($b, JSON_PRETTY_PRINT);

输出(更新以显示JSON)

[
    {
        "route": "300  PR. D. JO\u00c3O",
        "eta": "a passar",
        "waittime": ""
    },
    {
        "route": "904  Q.BELA VISTA",
        "eta": "a passar",
        "waittime": ""
    },
    {
        "route": "905  MONTE VIRGEM",
        "eta": "a passar",
        "waittime": ""
    },
    {
        "route": "300  TELHEIRA - C",
        "eta": "a passar",
        "waittime": ""
    },
    {
        "route": "200  CAST. QUEIJO",
        "eta": "20:39",
        "waittime": "5min"
    }
]