How to set an Arrays internal pointer to a specific position? PHP/XML

前端 未结 8 1464
花落未央
花落未央 2020-12-01 01:16

Im trying to build a little site using XML instead of a database.

I would like to build a next and prev button which will work relative to the content I have displa

8条回答
  •  天命终不由人
    2020-12-01 01:58

    Here's the complete @tjunglc 's approach with looping:

    protected function getPrevNext($aArray,$key)
    {
        $aKeys = array_keys($aArray); //every element of aKeys is obviously unique
        $aIndices = array_flip($aKeys); //so array can be flipped without risk
        $i = $aIndices[$key]; //index of key in aKeys
        if ($i > 0) $prev = $aArray[$aKeys[$i-1]]; //use previous key in aArray
        if ($i < count($aKeys)-1) $next = $aArray[$aKeys[$i+1]]; //use next key in aArray
        if (!isset($prev)) $prev = end($aArray);
        if (!isset($next)) $next = reset($aArray);
        return array($prev,$next);
    }
    

    Oh and thankx @tjunglc for this :)

提交回复
热议问题