php解析多个json文件,循环并在表中显示

php解析多个json文件,循环并在表中显示,php,json,parsing,loops,Php,Json,Parsing,Loops,我有一个脚本,可以多次解析json文件并在表中显示数据 要解析的文件由字符串组成,而不是由要解析的2值标记组成的filename.json 例如:www………../idr-gold.json(其中idr代表印度尼西亚货币) 国家是155个,所以我的ID是1到155,代表155个不同的标签 所以我做的是一个循环,每次运行添加+1到$x,所以$x被插入到一个脚本中,找到ID、countryName和countryCurrency之间的对应关系,这样每次循环运行我都会用丢失的标记构建155次url。

我有一个脚本,可以多次解析json文件并在表中显示数据

要解析的文件由字符串组成,而不是由要解析的2值标记组成的filename.json

例如:www………../idr-gold.json(其中idr代表印度尼西亚货币)

国家是155个,所以我的ID是1到155,代表155个不同的标签

所以我做的是一个循环,每次运行添加+1到$x,所以$x被插入到一个脚本中,找到ID、countryName和countryCurrency之间的对应关系,这样每次循环运行我都会用丢失的标记构建155次url。 然后脚本继续解析Json并返回一个表

问题有二: 1) 我希望在解析file.json之后显示表中的每一行(因此循环$x=1,解析www..idr-gold.json-->返回一个包含数据的表行;而不是$x=2,www..usd-gold.json等等)。 我尝试过移动脚本部分,但没有任何效果

2) 我需要解析的时间真的很长。。。是我的脚本的一部分,还是跨域分析太慢?似乎我不会每次运行都“关闭”或“清空”脚本,这样数据就会不断增加,从而加重负载。(关于这一点,我已经了解到这可能是一个跨服务器数据传输问题,因此我的解决方案将是第一个问题。如果我在页面运行结束时逐行显示组成表,而不是所有表,我将有一个php文件,它将平均显示1秒x 155次数据,尽管页面需要2分钟(1秒x 155次)在给你看任何东西之前

下面是代码:

<body>


<?php
// var declaration (here I have ex. $italyID = 7 and $italyCurrency = itl)
include 'var-declaration/var-declaration.php';

echo "<table class='db-table' width='1000'>";
echo "<tr>
<th width='14%'>Buy Currency</th>
<th width='14%'>Sell Currency</th>
<th width='10%'>Sell Rate</th>
<th width='12%'>Sell Amount</th>
<th width='15%'>Seller Name</th>
<th width='35%'>Link to Page</th>
</tr>";

// LOOP da 0 a 155
$x = 0; 
do{
// TEST    echo "The number is: $x <br>";
$iscountryID = ("$country".$x."");

$currencyVarName = ""; 
if ($iscountryID == 0) {
    $currencyVarName = $goldCurrency;
    } elseif ($iscountryID == 1) {
    $currencyVarName = $polandCurrency;
    } elseif ($iscountryID == 2) {
    $currencyVarName = $russiaCurrency;
    } elseif ($iscountryID == 3) {
    $currencyVarName = $germanyCurrency;
    } elseif ($iscountryID == 4) {
    $currencyVarName = $franceCurrency;
    } elseif ($iscountryID == 5) {
    $currencyVarName = $spainCurrency;
    } elseif ($iscountryID == 6) {
    $currencyVarName = $unitedKingdomCurrency;
    } elseif ($iscountryID == 7) {
    $currencyVarName = $italyCurrency;
    ... (up to 155)
    } elseif ($iscountryID == 154) {
    $currencyVarName = $bahrainCurrency;
    } elseif ($iscountryID == 155) {
    $currencyVarName = $kwaitCurrency;
    };

// if Currency name it isn't null
if ($currencyVarName != NULL) {
$urlLoopBuy =  strtolower("....exchange/".$currencyVarName."-gold.json");
$jsonLoopBuy = file_get_contents($urlLoopBuy);
$outLoopBuy = json_decode($jsonLoopBuy, true);

//json data parse
$buycurrency = $outLoopBuy['buy']['currency'];
$sellcurrency = $outLoopBuy['sell']['currency'];
$sellrate = $outLoopBuy['offer'][0]['rate'];
$sellamount = $outLoopBuy['offer'][0]['amount'];
$sellername = $outLoopBuy['offer'][0]['seller']['name'];

// table content
echo "<table class='db-table'  width='1000'>";
echo "<tr>
<td width='14%'>".$buycurrency."</td>
<td width='14%'>".$sellcurrency."</td>
<td width='10%'>".$sellrate."</td>
<td width='12%'>".$sellamount."</td>
<td width='15%'>".$sellername."</td>
<td width='35%'>".$urlLoopBuy."</td>
</tr></table>";

} else { // if currency name is NULL
$buycurrency = "record problem";
$sellcurrency = "record problem";
$sellrate = "record problem";
$sellamount = "record problem";
$sellername = "record problem";

//add +1 to loop
$x++;

};
} while ($x <= 155);


// Link to Page
$linktopage = "...monetarymarket.html?buyerCurrencyId=".$buyercountryID."&sellerCurrencyId=".$sellercountryID."";

?> 
</body>

你也能给我们提供一个json示例吗?当然可以。我用我的json示例编辑了主消息。我在问自己。在短时间内可能会有太多询问的问题。你认为如果我在每次循环运行后倒计时5秒,那么服务器intterogated有时间做“它必须做的事情”会有帮助吗?我正在尝试将所有代码转换为singole函数,但变化不大!
{"buy":
{"currency":"IDR","country":"Indonesia"},
"sell":{"currency":"GOLD","country":"Global"},
"offer":[{"amount":0.01,"rate":0.0931,
"seller":
{"id":7181,"type":"citizen","name":"Revenge Hero"}},{"amount":500,"rate":0.0931,"seller":
{"id":13,"type":"citizen","name":"China Org"}},{"amount":30.18999,"rate":0.0932,"seller":
{"id":479821,"type":"citizen","name":"GGininDer"}},{"amount":2305,"rate":0.0999,"seller":
{"id":2019,"type":"citizen","name":"rockbein"}},{"amount":808.43,"rate":0.1,"seller":
{"id":8622,"type":"citizen","name":"haarp"}},{"amount":6703.03,"rate":0.1199,"seller":
{"id":7481,"type":"citizen","name":"edulin"}},{"amount":1,"rate":1,"seller":
{"id":479821,"type":"citizen","name":"GGininDer"}},{"amount":1,"rate":1,"seller":
{"id":144942,"type":"citizen","name":"TW_Jimmy"}}],
"load":1.5037939548492}