Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP将巨大的json数据解析为html速度很慢_Php_Laravel 5 - Fatal编程技术网

用PHP将巨大的json数据解析为html速度很慢

用PHP将巨大的json数据解析为html速度很慢,php,laravel-5,Php,Laravel 5,我对庞大的JSON数据有一个问题。我使用服务从后端获取json数据,然后使用PHP将数据解析为HTML 当我获取原始JSON数据时需要2秒,但当我将其解析为HTML时,需要的时间太长,大约35秒。 如何加快解析时间 在下面找到我的密码。我使用递归函数和for函数中的循环 $allstructure=$this->allStructure(); $str='<ul style="padding: 0px;padding-left: 5px;list-style: none"&

我对庞大的JSON数据有一个问题。我使用服务从后端获取json数据,然后使用PHP将数据解析为HTML

当我获取原始JSON数据时需要2秒,但当我将其解析为HTML时,需要的时间太长,大约35秒。 如何加快解析时间

在下面找到我的密码。我使用递归函数和for函数中的循环

$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;

}
$allstructure=$this->allstructure();
$str='
    ; 对于($x=0;$xname.”; $str.='
  • 。$this->iterator($allstructure[$x]->child)。'
  • ; } $str.='
'; 返回$str; } 公共函数迭代器($data) { $str='
    '; 对于($i=0;$iname; $str.=“
      ”; $str.='
    • ; 对于($z=0;$zlistPosNames);$z++){ $str.='
    • .$data[$i]->listPosNames[$z]->posName.

      '.$this->createHtml($data[$i]->listPosNames[$z]->posNameId.
    • '; } $str.='
    '; if(isset($data[$i]->child)){ $str.=$this->iterator($data[$i]->child); } $str.=''; } $str.='
'; 返回$str; }
在上面的代码中,您最大的问题是使用
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>';

}
$structureCount=count($allstructure);
对于($x=0;$xname.”;
$str.='
  • 。$this->iterator($allstructure[$x]->child)。“
  • ”; }
    这方面的一个很好的资源是。对于这个问题,它表明每次调用
    count()
    所需的时间大约是调用一次所需时间的340%,就像我上面所做的那样。因为你有三个这样的循环有效嵌套,这一变化意味着你的应用程序可能需要大约3.43=39倍的时间

    此外,您可能不想一次向用户显示“巨大”的数据。您应该考虑分页(即,只显示一部分数据)。

    <代码>公共函数CREATED HTML($ID){ $html1=''; $countDir=Direction::orderBy('id','desc')->count(); $collection=Direction::orderBy('id','desc')->get(); 对于($x=0;$xid)->count(); $html1.=' '; $html1.='

    。$direct->name。”

    '; $countakts=Akt::where('direction_id',$direct->id)->count(); $aktcollect=Akt::where('direction_id',$direct->id)->get(); 对于($y=0;$yid.[]”value=“”。$akt->id。“”>”。$akt->name。“”; } $html1.=' “.$direct['name']”。

    Seçin; foreach(Akt::where('direction\u id',$direct->id)->get()作为$Akt){ $html1.=''.$akt['name'].'; } $html1.=' '; } $html1.=''; 返回$html1; }


    请详细说明“获取原始JSON数据需要2秒钟”的含义。我假设“将其解析为HTML”"你的意思是当你循环整个JSON
    $allstructure
    以PHP输出HTML时?一件显而易见的事情是将JSON切成更小的块。如果数据很大,那么我认为在浏览器中向用户显示这么长的列表没有什么意义。我按照你说的做了。但没有什么影响。@AzerQurbanov你修改了所有的内容吗你的
    for
    循环有多少?这三个循环都需要更改,你应该会看到很大的影响。这些数据到底有多大?@AzerQurbanov,你有没有测试过解析本身需要多长时间?我假设你使用的是
    json_decode()
    。这需要多长时间?是的,我使用json_解码。我使用cUrl向服务请求。它在2秒内返回数据而不解析为HTML。然后我获取数据并使用PHP(Laravel)将其解析为HTML。我从迭代函数中删除了此代码,然后它加载了2.5秒。$this->createHtml($data[$I]->listPosNames[$z]->posNameId)。'
    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;
    
    }