0

hi have this procedure to create an array chain of news

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");
            }
        }
    }
}

this procedure return an correct array but with reset keys. how can I do to preserve the indexes?

1
  • Thank you. Another question how can I turn it into a function? Commented Mar 9, 2012 at 12:09

3 Answers 3

2

use

array_reverse($chain, true);

Read more about array_reverse

Sign up to request clarification or add additional context in comments.

Comments

1

You need to preserve the keys when you do array_reverse­Docs:

$chain = array_reverse($chain, TRUE);

By default, without the second parameter, keys are not preserved.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.