我的PHP脚本花费的时间太长以回显文件中的数据。似乎我出现了一些循环错误,并且处理时间太长。请任何人都看看我的代码,并让我知道我哪里出错了? 我的任务是遍历JSON数据数组并回显XML标记中的数据。 我的json数组看起来像下面的 [[“GENERIC SALES PRICE”,“”],[“REGION”,“SALE PRICE”],[“AMERICA”,“260,000”],[“ASIA”,“340,000”] ..等]PHP脚本花费太长时间来回显数据
$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);
请帮助我, 在此先感谢
文件有多大?即文件大小(mb/gb)和行数 – SmokeyPHP
输入文件仅为10 * 2阵列。输出xml文件包含〜20行 – user2791530
由于您在本地计算机上(Windows),请尝试使用反斜杠'$ dir =“C:\ .. \ data \ GenericSalePrice.xml”;'你甚至可能需要使用双反斜杠。 –