[英]PHP script taking too long to echo the data
My PHP script is taking too long to echo the data in a file. 我的PHP脚本花费太长时间来回显文件中的数据。 It seems that I have gone wrong with some loops and its taking too long to process.Could anyone please look at my code and let me know where i went wrong?
似乎我在某些循环上出错了,并且处理时间太长了。有人可以看一下我的代码,让我知道我哪里出错了吗? My task is to loop through the JSON data array and echo the data in XML tags.
我的任务是遍历JSON数据数组并回显XML标记中的数据。 My json array looks like following [["GENERIC SALES PRICE", ""],["REGION", "SALE PRICE"],["AMERICA", "260,000"],["ASIA","340,000"],..etc]
我的json数组如下[[“ GENERIC SALES PRICE”,“”],[“ REGION”,“ SALE PRICE”],[“ AMERICA”,“ 260,000”],[“ ASIA”,“ 340,000”],。 。等等]
$fp = fopen("data_save2.json", "r");
$inp=file_get_contents("data_save2.json");
fclose($fp);
$inp=json_decode($inp);
$inp_info=$inp;
$start_row=0;
$start_col=0;
for($i=0; $i <= $inp_info; $i++) {
for($j=0; $j <= $inp_info; $j++) {
if ($inp_info[$i][$j]=='GENERIC SALES PRICE') {
$start_row=$i+1;
$start_col=$j;
}
$row_index=$start_row;
$rows_of_data=0;
while($inp_info[$row_index][$start_col]!=''){
$rows_of_data = $rows_of_data+1;
$row_index = $row_index+1;}
}
}
$dir = "C:/../data/GenericSalePrice.xml";
$fp = fopen($dir, 'w');
fwrite($fp,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
fwrite($fp,"\n");
fwrite($fp,'<generic-sales-price xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">');
fwrite($fp,"\n");
for($j=0; $j<=$rows_of_data; $j++){
fwrite($fp,"\t<sale-price>\n");
$region = $inp_info[$start_row+$j][$start_col];
fwrite($fp, "\t\t<region>");
fwrite($fp,"$region");
fwrite($fp, "</region>\n");
$price =$inp_info[$start_row+$j][$start_col+1];
fwrite($fp, "\t\t<price>");
fwrite($fp, "$price");
fwrite($fp, "</price>\n");
fwrite($fp, "\t</sale-price>\n");
}
fwrite($fp, "</generic-sales-price>");
fclose($fp);
Please help me with this, Thanks in advance 请帮助我,在此先谢谢
WARNING - Going out on a limb here and using this as my answer without testing to confirm speed or syntax without proper data. 警告-请在此处四肢走动,将其用作我的答案,而无需进行测试即可在没有适当数据的情况下确认速度或语法。
$xml_file = "C:/../data/GenericSalePrice.xml";
$header =
<<<EOT
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<generic-sales-price xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
EOT;
$body = '';
for($j=0; $j<=$rows_of_data; $j++) {
$region = $inp_info[$start_row+$j][$start_col];
$price = $inp_info[$start_row+$j][$start_col+1];
$temp_body =
<<<EOT
<sale-price>
<region>
{$region}
</region>
<price>
{$price}
</price>
</sale-price>
EOT;
$body .= $temp_body;
}
$footer = "</generic-sales-price>";
$string = $header . $body . $footer;
file_put_contents($xml_file, $string);
Found the bug.. I was using 找到了错误。。我正在使用
for($i=0; $i <= $inp_info; $i++) {
for($j=0; $j <= $inp_info; $j++) {
Now, I have changed it to 现在,我将其更改为
for($i=0; $i <= count($inp_info); $i++) {
for($j=0; $j <= count($inp_info); $j++) {
It started working. 它开始工作了。 Thanks everyone for your time and effort
感谢大家的时间和精力
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.