Moving between items of an array - probably simple!

I've got a set of links for my menu in an array:

$aboutlinks = array (
      "About us" => "/aboutus/aboutus.$ext",
      "Client portfolio" => "/aboutus/clients.$ext",
      "Serious about quality" => "/aboutus/quality.$ext"
);

I have a function that tries to create the following string for links to the previous and next links (Assuming you're currently on the Client Portfolio page:

<- About us | Serious about quality ->

The function thus far is below:

      function printnextprevlinks ($arrayofitems) {
            $currentpage = explode("?", basename($_SERVER[PHP_SELF]));
            $currentpage = $currentpage[0];
            foreach ($arrayofitems as $linktext=>$link) {
                  $linkpage = explode("?", basename($link));
                  $linkpage = $linkpage[0];
                  if ($linkpage == $currentpage) {
                        $currentlink = array_search($link,$arrayofitems);
                        $prevlink = prev($arrayofitems);
                        $nextlink = next($arrayofitems);
                  }
            }
            echo "<div class=\"breadcrumbs\">&laquo; $prevlink | $nextlink &raquo;</div>";
      }

I was expecting array_search to return the item position, so I could just use prev() and next(), but it actually return the key name (i.e. the name of the page), so 'm a bit stuck. Any help would be greatly appreciated!

Cheers,

theskintman
theskintmanAsked:
Who is Participating?
 
steelseth12Commented:
HI theskintman

Try this

function printnextprevlinks ($arrayofitems) {

      $arrayvalues = array_values($arrayofitems);
      
      $arraykeys = array_keys($arrayofitems);
      
      $currentpage = explode("?", basename($_SERVER[PHP_SELF]));
      
      $currentpage = $currentpage[0];
      
      foreach ($arrayvalues as $key=>$link) {
      
            $linktext = $arraykeys[$key];
            
            $linkpage = explode("?", basename($link));
            
            $linkpage = $linkpage[0];
            
            if ($linkpage == $currentpage) {
            
                  $prevlink = $arrayvalues[$key-1];
                  
                  $nextlink = $arrayvalues[$key+1];
      
            }
      
      }
      echo "<div class=\"breadcrumbs\">&laquo; $prevlink | $nextlink &raquo;</div>";
}       
0
 
rschoenbachCommented:
If you want it to return the item position you n like this..

$aboutlinks = array(
                         array("About us" => "/aboutus/aboutus.$ext"),
                         array("Client portfolio" => "/aboutus/clients.$ext"),
                         array("Serious about quality" => "/aboutus/quality.$ext")
0
 
rschoenbachCommented:
opps something happened to what i wrote.

If you want it to return the item position you will want to create the numeric array first then create a multidimensional array like this.

$aboutlinks = array(
                         array("About us" => "/aboutus/aboutus.$ext"),
                         array("Client portfolio" => "/aboutus/clients.$ext"),
                         array("Serious about quality" => "/aboutus/quality.$ext")
0
 
theskintmanAuthor Commented:
Thanks, but I have loads of other function which rely on this array being as it is - would not fancy altering them all to work with multi-dimentional arrays!

Is there any way in which I can set the current item in the array and just use prev() and next(), like:

foreach ($arrayofitems as $linktext=>$link) {
      $prevlink = prev($arrayofitems);
}

This doesn't work for me.

Or just get the index of the item returned from array_search, rather than the key?
0
 
theskintmanAuthor Commented:
Thanks, steelseth12 - works a treat!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.