2013-10-09 89 views
1

我的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); 

请帮助我, 在此先感谢

+0

文件有多大?即文件大小(mb/gb)和行数 – SmokeyPHP

+0

输入文件仅为10 * 2阵列。输出xml文件包含〜20行 – user2791530

+1

由于您在本地计算机上(Windows),请尝试使用反斜杠'$ dir =“C:\ .. \ data \ GenericSalePrice.xml”;'你甚至可能需要使用双反斜杠。 –

回答

0

发现的bug .. 我用

for($i=0; $i <= $inp_info; $i++) { 
for($j=0; $j <= $inp_info; $j++) { 

现在,我已经把它改为

for($i=0; $i <= count($inp_info); $i++) { 
for($j=0; $j <= count($inp_info); $j++) { 

它开始工作。谢谢大家的时间和精力

+1

如果你把这个标记为你的问题的答案,这将是很好的。 – idmean

+0

该网站说,我可以在2天后接受我自己的答案:( – user2791530

0

警告 - 在肢体上走出这里用这个作为我的回答没有测试,以确认速度或语法,而不正确的数据。

$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); 
相关问题