2017-05-20 40 views
0

我有一個巨大的JSON數據問題。我從服務後端獲取json數據,然後使用PHP將數據解析爲HTML。用PHP解析巨大的json數據到html很慢

當我需要2個第二胎JSON數據,但是當我解析成HTML,時間過久,在35秒左右。 如何加快解析時間?

找到我的代碼波紋管。我使用遞歸函數和該函數內的循環。

$allstructure=$this->allStructure(); 

    $str='<ul style="padding: 0px;padding-left: 5px;list-style: none">'; 
    for ($x=0;$x<count($allstructure);$x++){ 

     $str.='<li>'.$allstructure[$x]->name.'</li>'; 
     $str.='<li>'.$this->iterator($allstructure[$x]->child).'</li>'; 

    } 

    $str.='</ul>'; 

    return $str; 
} 
    public function iterator($data) 
{ 

    $str='<ul>'; 

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

     $str.='<li>'.$data[$i]->name; 

     $str.='<ul>'; 

     $str.='<li style="display:flex"><input type="checkbox" class="rehbersecim"></li>'; 

     for($z=0;$z<count($data[$i]->listPosNames);$z++){ 

      $str.='<li style="display:flex"><input type="checkbox" class="checkqutu" name="vezife[]" value="'.$data[$i]->listPosNames[$z]->posNameId.'"><p style="width:230px;height:20px"><b>'.$data[$i]->listPosNames[$z]->posName.'</b></p> '.$this->createHtml($data[$i]->listPosNames[$z]->posNameId).'</li>'; 

     } 

     $str.='</ul>'; 

     if(isset($data[$i]->child)){ 

      $str.=$this->iterator($data[$i]->child); 
     } 
     $str.='</li>'; 

    } 

    $str.='</ul>'; 

    return $str; 

} 
+1

請詳細說明「當我得到原始JSON數據需要2秒」的含義。我假設「將它解析爲HTML」是指當您遍歷整個JSON'$ allstructure'來輸出PHP中的HTML時? 一個顯而易見的事情就是將JSON切割成更小的塊。如果數據很大,那麼我認爲在瀏覽器中向用戶顯示如此長的列表是沒有意義的。 – TomTom101

回答

0

您上面代碼中最大的問題是使用for(...; count(...); ...)。這會迫使PHP在每次遇到有問題的循環時執行count(),並且您有三個這樣的問題。在處理大量數據時,這將會非常緩慢。

而不是做這種方式的,請撥打count()一次每個循環,就像這樣:

$structureCount = count($allstructure); 
for ($x=0; $x<$structureCount; $x++){ 

    $str .= '<li>' . $allstructure[$x]->name . '</li>'; 
    $str .= '<li>' . $this->iterator($allstructure[$x]->child) . '</li>'; 

} 

對於這種事情一個很好的資源是http://www.phpbench.com/。對於這個問題,它顯示每次調用count()只需要調用一次,大約需要340%,就像我上面做的那樣。因爲你有3個這樣的循環有效地嵌套,這個變化意味着你的應用程序可能需要約3.4倍的時間。

而且,TomTom101 pointed out in the comments,你可能不希望表現出「巨大的」數據傳送給用戶的一次。您應該考慮分頁(即一次只顯示部分數據)。

+0

我照你說的去做了。但影響很小。 –

+0

@AzerQurbanov你是否修改了所有的'for'循環?所有這三點都需要改變,你會看到非常大的影響。這數據是多麼「巨大」? –

+0

@AzerQurbanov另外,你有沒有基準解析本身需要多長時間?我假設你正在使用'json_decode()'。這需要多長時間?當然是 –

0
public function createHtml($id){ 

    $html1='<table style="margin-top: 0px;float: right">'; 

    $countDir=Direction::orderBy('id', 'desc')->count(); 
    $collection=Direction::orderBy('id', 'desc')->get(); 
    for($x=0;$x<$countDir;$x++){ 

     $direct=$collection[$x]; 
     $say=Akt::where('direction_id', $direct->id)->count(); 
     $html1.='<tr class="az"><td><input type="checkbox" class="tick tickcount" name="checkn'.$id.'[]" rel="'.$id.'" value="'.$direct->id.'"> 
     <input type="text" class="form-control countbox countbox'.$id.'" value="0" rel="'.$direct->id.'" style="float:right;width:50px"> 
     <input type="hidden" name="say" class="say" value="'.$say.'" style="width:50px"> 
     </td><td><input type="button" value="AKTS" class="btn btn-default" data-popup-open="popup-1">'; 

     $html1.='<div class="popup" data-popup="popup-1"> 
    <div class="popup-inner"> 
     <div> 
      <div style="width:220px;float: left;"> 
       <p style="margin: 0px !important;display:block;width:270px;height:40px;text-align: center;font-weight: bold">'.$direct->name.'</p> 
       <table>'; 
     $countakts=Akt::where('direction_id', $direct->id)->count(); 
     $aktcollect=Akt::where('direction_id', $direct->id)->get(); 
     for($y=0;$y<$countakts;$y++) { 

      $akt=$aktcollect[$y]; 
      $html1 .= '<tr><td><input type="checkbox" class="aktclass" name="akt'.$id.$direct->id.'[]" value="' . $akt->id . '"></td><td>' . $akt->name . '</td></tr>'; 
     } 
     $html1.='</table> 
      </div> 
     </div> 

     <a class="popup-close" data-popup-close="popup-1" href="#">x</a> 
    </div> 


</div></td><td style="width: 300px">'.$direct['name'].'<p class="mesaj" style="font-size:10px;color:red"></p></td><td><table><tr><td><table class="prtable"></table></td><td><select class="prsecimn form-control" style="padding-left:2px;padding-right:2px;width:50px !important"><option value="0">Seçin..</option>'; 

     foreach(Akt::where('direction_id', $direct->id)->get() as $akt){ 
      $html1.='<option value="'.$akt['id'].'">'.$akt['name'].'</option>'; 
     } 

     $html1.='</select></td><td><!--<input style="width:50px" type="text" rel="'.$id.'" class="prsecimcount">--></td> 
               <td><input type="button" rel="'.$direct->id.'" value="+" class="addprsecim form-control"></td></tr> 
              </table></td></tr>'; 
    } 

    $html1.='</table>'; 

    return $html1; 

}