2012-03-09 66 views
0

数组键喜有此程序建立的新闻PHP保存递归过程

public function getNewsChain(&$chain, $itemID,$langID, $direction="prev") { 
    $langID = $this->db->lngPatch($langID); 
    $where = ($direction == "prev") ? "n.ID='$itemID'" : "ln.Rif='$itemID'"; 
    $qr = "SELECT n.ID, n.idSS, n.Data_News AS startDate, n.evento AS event, n.Data_Fine AS endDate, n.rifGeog, 
        ln.Titolo, ln.Corpo, ln.Link AS link, ln.Pretitolo, ln.sottoTitolo, ln.Immagine, ln.Rif, ln.idLng 
      FROM news n LEFT JOIN lngnews ln ON n.ID=ln.idNews 
      WHERE $where AND ln.idLng='$langID'"; 
    if ($rs = $this->db->exQ($qr,$this->src, true, false)) { 
     $row = $this->db->fetch($rs['RS']); 
     $chain[$row['ID']] = array("itemID"=>$row['ID'], 
          "langID"=>$this->db->lngPatchRev($row['idLng']), 
          "ssID"=>$row['idSS'], 
          "startDate"=>$row['startDate'], 
          "event"=>$row['event'], 
          "endDate"=>$row['endDate'], 
          "title"=>$row['Titolo'], 
          "body"=>$row['Corpo'], 
          "link"=>$row['link'], 
          "pretitle"=>$row['Pretitolo'], 
          "subtitle"=>$row['sottoTitolo'], 
          "geoRefer"=>$row['rifGeog'], 
          "image"=>$row['Immagine'], 
          "rif"=>$row['Rif']); 
      if ($row['Rif'] != 0) { 
       $this->getNewsChain($chain, $row['Rif'],$row['idLng'], "prev"); 
      } else { 
       $chain = array_reverse($chain); 
       $this->getNewsChain($chain, $chain[count($chain)-1]['itemID'], $row['idLng'], "next"); 
      } 
     } else { 
      if ($row['Rif'] != 0) { 
       $this->getNewsChain($chain, $row['ID'], $row['idLng'], "next"); 
      } 
     } 
    } 
} 

的阵列链式此过程返回一个正确的数组,但有复位键。 我该如何保存索引?

+0

谢谢。另一个问题我怎么能把它变成一个函数? – user1259211 2012-03-09 12:09:39

回答

1

你需要,当你做array_reverse­Docs保存键:

$chain = array_reverse($chain, TRUE); 

默认情况下,如果没有第二个参数,按键不会保留。

1
array_reverse($chain); --> array_reverse($chain, true); 
2

使用

array_reverse($chain, true); 

了解更多关于array_reverse