我有一个连接到数据库并为我获取一些资源的 php 文件。
在 while 循环中,我将数据附加到 php 数组中。最后,json_encode
与这个结果数组一起使用。
现在,我正在使用array_push
,但是对于它被调用的次数,脚本非常慢!我试图更改为“[]”以附加到最后一个元素,但我没有注意到性能有任何变化。
我的问题是:有没有更好的方法(而且更快!)。我也尝试过array_splice
and array_merge
,但没有运气,
编辑: 通过评论 array_push 部分(但访问相同的行数和次数)脚本很快。所以,这让我认为瓶颈在于推动。
编辑2:
编码
$Tema=-1;
$SubTema=-1;
$Topico=-1;
$SubTopico=-1;
$RS = mysql_query($SQL);
$respArray['items'] = array();
$resLevel=-1;
$UniqueKey=1;
$TemaId = '';
$TemaOpen = '';
$TemaActivo = '';
$TemaIsRecurso = '';
$TemaTipoRecurso = '';
$SubtemaId = '';
$SubtemaOpen = '';
$SubtemaActivo = '';
$SubtemaIsRecurso = '';
$SubtemaTipoRecurso = '';
$TopicoId = '';
$TopicoOpen = '';
$TopicoActivo = '';
$TopicoIsRecurso = '';
$TopicoTipoRecurso = '';
$SubtopicoId = '';
$SubtopicoOpen = '';
$SubtopicoIsRecurso = '';
$SubtopicoTipoRecurso = '';
while($row = mysql_fetch_array($RS))
{
if(!is_null($row['IDTEMA'])){
if($Tema!=$row['IDTEMA']){
$TemaId = $row['IDTEMA'];
$TemaOpen = '1';
$TemaActivo = $row['MACTIVO'];
$TemaTitle = $row['TEMA'];
$TemaIsRecurso = '0';
$TemaTipoRecurso = '';
$Tema=$row['IDTEMA'];
$SubTema=-1;
$respArray['items'][] = array ('Id' => $TemaId, 'isRecurso' => '0', 'Level' => '1', 'Title' => $TemaTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() );
continue;
//array_push($respArray['items'], array ('Id' => $TemaId, 'isRecurso' => '0', 'Level' => '1', 'Title' => $TemaTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() ));
//echo("TEMA ".$TemaTitle."\n");
}
if($row['IDSUBTEMA']!=""){
if($SubTema!=$row['IDSUBTEMA']){
$SubtemaId = $row['IDSUBTEMA'];
$SubtemaOpen = '1';
$SubtemaActivo = $row['MACTIVO'];
$SubtemaTitle = $row['SUBTEMA'];
$SubtemaIsRecurso = '0';
$SubtemaTipoRecurso = '';
$SubTema=$row['IDSUBTEMA'];
$Topico=-1;
$numTemas = count($respArray['items'])-1;
$respArray['items'][$numTemas]['children_items'][] = array ('Id' => $SubtemaId, 'isRecurso' => '0', 'Level' => '2', 'Title' => $SubtemaTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() );
continue;
}
if($row['IDTOPICO']!=""){
if($Topico!=$row['IDTOPICO']){
$TopicoId = $row['IDTOPICO'];
$TopicoOpen = '1';
$TopicoActivo = $row['MACTIVO'];
$TopicoTitle = $row['TOPICO'];
$TopicoIsRecurso = '0';
$TopicoTipoRecurso = '';
$Topico=$row['IDTOPICO'];
$SubTopico=-1;
$numTemas = count($respArray['items'])-1;
$numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
$respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][] = array ('Id' => $TopicoId, 'isRecurso' => '0', 'Level' => '3', 'Title' => $TopicoTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() );
continue;
}
if($row['IDSUBTOPICO']!=""){
if($SubTopico!=$row['IDSUBTOPICO']){
$SubtopicoId = $row['IDTOPICO'];
$SubtopicoOpen = '1';
$SubtopicoActivo = $row['MACTIVO'];
$SubtopicoTitle = $row['SUBTOPICO'];
$SubtopicoIsRecurso = '0';
$SubtopicoTipoRecurso = '';
$SubTopico=$row['IDSUBTOPICO'];
$numTemas = count($respArray['items'])-1;
$numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
$numTopicos = count($respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'])-1;
$respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][$numTopicos]['children_items'][] = array ('Id' => $SubtopicoId, 'Level' => '4', 'isRecurso' => '0', 'Title' => $SubtopicoTitle, 'UniqueKey' => $UniqueKey++, 'children_items' => array() );
continue;
}
if($row['IDRECURSO']!=""){
$resId = $row['IDRECURSO'];
$resLevel = '5';
$resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
$resIcon = $row['ICON'];
$resCat = $row['IDCATEGORIA'];
$resFormato = $row['IDFORMATO'];
$resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ? 1 : 0;
$resInfo = $row['INFORMACAO'];
$resDown = $row['GRAVACAO'];
$resFicD = $row['FICDOWNLOAD'];
$resGuia = $row['GUIA'];
$resVol = $row['VOLUME'];
$resIdFavorito = $row['IDFAVORITO'];
$resActivo = $row['MACTIVO'];
$resTempoGasto = $row['TEMPOGASTO'];
$resNumSessoes = $row['NUMSESSOES'];
$resLastDate = $row['LASTDATE'];
$resScore = $row['SCORE'];
$resProgresso = $row['PROGRESSO'];
$resTitle = utf8_encode($row['RECURSO']);
$resIsRecurso = '1';
$numTemas = count($respArray['items'])-1;
$numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
$numTopicos = count($respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'])-1;
$numSubTopicos = count($respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][$numTopicos]['children_items'])-1;
$respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][$numTopicos]['children_items'][$numSubTopicos]['children_items'][] = array (
'Id' => $resId,
'Level' => $resLevel,
'Fic' => $resFic,
"Icon" => $resIcon,
"Cat" => $resCat,
"Formato" => $resFormato,
"Prof" => $resProf,
"Info" => $resInfo,
"Down" => $resDown,
"FicD" => $resFicD,
"Guia" => $resGuia,
"Vol" => $resVol,
"IdFavorito" => $resIdFavorito,
"Activo" => $resActivo,
"TempoGasto" => $resTempoGasto,
"NumSessoes" => $resNumSessoes,
"LastDate" => $resLastDate,
"Score" => $resScore,
"Progresso" => $resProgresso,
'Title' => $resTitle,
'isRecurso' => '1',
'colorIcon' => '#FF2200',
'tipoRecurso' => 'tipo_recurso',
'UniqueKey' => $UniqueKey++,
'children_items' => null );
continue;
}
}else{
if($row['IDRECURSO']!=""){
$resId = $row['IDRECURSO'];
$resLevel = '4';
$resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
$resIcon = $row['ICON'];
$resCat = $row['IDCATEGORIA'];
$resFormato = $row['IDFORMATO'];
$resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ? 1 : 0;
$resInfo = $row['INFORMACAO'];
$resDown = $row['GRAVACAO'];
$resFicD = $row['FICDOWNLOAD'];
$resGuia = $row['GUIA'];
$resVol = $row['VOLUME'];
$resIdFavorito = $row['IDFAVORITO'];
$resActivo = $row['MACTIVO'];
$resTempoGasto = $row['TEMPOGASTO'];
$resNumSessoes = $row['NUMSESSOES'];
$resLastDate = $row['LASTDATE'];
$resScore = $row['SCORE'];
$resProgresso = $row['PROGRESSO'];
$resTitle = utf8_encode($row['RECURSO']);
$resIsRecurso = '1';
$numTemas = count($respArray['items'])-1;
$numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
$numTopicos = count($respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'])-1;
$respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][$numTopicos]['children_items'][] = array (
'Id' => $resId,
'Level' => $resLevel,
'Fic' => $resFic,
"Icon" => $resIcon,
"Cat" => $resCat,
"Formato" => $resFormato,
"Prof" => $resProf,
"Info" => $resInfo,
"Down" => $resDown,
"FicD" => $resFicD,
"Guia" => $resGuia,
"Vol" => $resVol,
"IdFavorito" => $resIdFavorito,
"Activo" => $resActivo,
"TempoGasto" => $resTempoGasto,
"NumSessoes" => $resNumSessoes,
"LastDate" => $resLastDate,
"Score" => $resScore,
"Progresso" => $resProgresso,
'Title' => $resTitle,
'isRecurso' => '1',
'colorIcon' => '#FF2200',
'tipoRecurso' => 'tipo_recurso',
'UniqueKey' => $UniqueKey++,
'children_items' => null );
continue;
}
}
}else{
if($row['IDRECURSO']!="" && $row['IDSUBTEMA']!=""){
$resId = $row['IDRECURSO'];
$resLevel = '3';
$resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
$resIcon = $row['ICON'];
$resCat = $row['IDCATEGORIA'];
$resFormato = $row['IDFORMATO'];
$resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ? 1 : 0;
$resInfo = $row['INFORMACAO'];
$resDown = $row['GRAVACAO'];
$resFicD = $row['FICDOWNLOAD'];
$resGuia = $row['GUIA'];
$resVol = $row['VOLUME'];
$resIdFavorito = $row['IDFAVORITO'];
$resActivo = $row['MACTIVO'];
$resTempoGasto = $row['TEMPOGASTO'];
$resNumSessoes = $row['NUMSESSOES'];
$resLastDate = $row['LASTDATE'];
$resScore = $row['SCORE'];
$resProgresso = $row['PROGRESSO'];
$resTitle = utf8_encode($row['RECURSO']);
$resIsRecurso = '1';
$numTemas = count($respArray['items'])-1;
$numSubTemas = count($respArray['items'][$numTemas]['children_items'])-1;
$respArray['items'][$numTemas]['children_items'][$numSubTemas]['children_items'][] = array (
'Id' => $resId,
'Level' => $resLevel,
'Fic' => $resFic,
"Icon" => $resIcon,
"Cat" => $resCat,
"Formato" => $resFormato,
"Prof" => $resProf,
"Info" => $resInfo,
"Down" => $resDown,
"FicD" => $resFicD,
"Guia" => $resGuia,
"Vol" => $resVol,
"IdFavorito" => $resIdFavorito,
"Activo" => $resActivo,
"TempoGasto" => $resTempoGasto,
"NumSessoes" => $resNumSessoes,
"LastDate" => $resLastDate,
"Score" => $resScore,
"Progresso" => $resProgresso,
'Title' => $resTitle,
'isRecurso' => '1',
'colorIcon' => '#FF2200',
'tipoRecurso' => 'tipo_recurso',
'UniqueKey' => $UniqueKey++,
'children_items' => null );
continue;
}
}
}else{
if($row['IDRECURSO']!=""){
$resId = $row['IDRECURSO'];
$resLevel = '2';
$resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
$resIcon = $row['ICON'];
$resCat = $row['IDCATEGORIA'];
$resFormato = $row['IDFORMATO'];
$resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ? 1 : 0;
$resInfo = $row['INFORMACAO'];
$resDown = $row['GRAVACAO'];
$resFicD = $row['FICDOWNLOAD'];
$resGuia = $row['GUIA'];
$resVol = $row['VOLUME'];
$resIdFavorito = $row['IDFAVORITO'];
$resActivo = $row['MACTIVO'];
$resTempoGasto = $row['TEMPOGASTO'];
$resNumSessoes = $row['NUMSESSOES'];
$resLastDate = $row['LASTDATE'];
$resScore = $row['SCORE'];
$resProgresso = $row['PROGRESSO'];
$resTitle = utf8_encode($row['RECURSO']);
$resIsRecurso = '1';
$numTemas = count($respArray['items'])-1;
$respArray['items'][$numTemas]['children_items'][] = array (
'Id' => $resId,
'Level' => $resLevel,
'Fic' => $resFic,
"Icon" => $resIcon,
"Cat" => $resCat,
"Formato" => $resFormato,
"Prof" => $resProf,
"Info" => $resInfo,
"Down" => $resDown,
"FicD" => $resFicD,
"Guia" => $resGuia,
"Vol" => $resVol,
"IdFavorito" => $resIdFavorito,
"Activo" => $resActivo,
"TempoGasto" => $resTempoGasto,
"NumSessoes" => $resNumSessoes,
"LastDate" => $resLastDate,
"Score" => $resScore,
"Progresso" => $resProgresso,
'Title' => $resTitle,
'isRecurso' => '1',
'colorIcon' => '#FF2200',
'tipoRecurso' => 'tipo_recurso',
'UniqueKey' => $UniqueKey++,
'children_items' => null );
continue;
}
}
}else{
if($row['IDRECURSO']!=""){
$resId = $row['IDRECURSO'];
$resLevel = '1';
$resFic = ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PUBSEC) ? $row['FICSRC'] . "$" . $row['FICVOL'] : ((($row['IDCATEGORIA']==$BD->ID_CATEGORIA_LINKS) || ($row['IDCATEGORIA']==$BD->ID_CATEGORIA_PAGINAS)) ? $row['FICSRC'] : $row['FICHEIRO']);
$resIcon = $row['ICON'];
$resCat = $row['IDCATEGORIA'];
$resFormato = $row['IDFORMATO'];
$resProf = (($row['IDDESTINATARIO']==3) || ($row['IDDESTINATARIO']==4)) ? 1 : 0;
$resInfo = $row['INFORMACAO'];
$resDown = $row['GRAVACAO'];
$resFicD = $row['FICDOWNLOAD'];
$resGuia = $row['GUIA'];
$resVol = $row['VOLUME'];
$resIdFavorito = $row['IDFAVORITO'];
$resActivo = $row['MACTIVO'];
$resTempoGasto = $row['TEMPOGASTO'];
$resNumSessoes = $row['NUMSESSOES'];
$resLastDate = $row['LASTDATE'];
$resScore = $row['SCORE'];
$resProgresso = $row['PROGRESSO'];
$resTitle = utf8_encode($row['RECURSO']);
$resIsRecurso = '1';
$respArray['data']['items'][] = array (
"Id" => $resId,
'Level' => $resLevel,
"Fic" => $resFic,
"Icon" => $resIcon,
"Cat" => $resCat,
"Formato" => $resFormato,
"Prof" => $resProf,
"Info" => $resInfo,
"Down" => $resDown,
"FicD" => $resFicD,
"Guia" => $resGuia,
"Vol" => $resVol,
"IdFavorito" => $resIdFavorito,
"Activo" => $resActivo,
"TempoGasto" => $resTempoGasto,
"NumSessoes" => $resNumSessoes,
"LastDate" => $resLastDate,
"Score" => $resScore,
"Progresso" => $resProgresso,
'Title' => $resTitle,
'isRecurso' => '1',
'colorIcon' => '#FF2200',
'tipoRecurso' => 'tipo_recurso',
'UniqueKey' => $UniqueKey++,
'children_items' => null );
continue;
}
}
}
mysql_close($link);
echo json_encode($respArray) ;
}
谢谢