Finding the next element in an array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Sorry for posting from Google Groups - my news hosts is fubar right

Let's say my page is set up so:

$thispage = "This goes in the title element";
$thisurl = "sample.php";

I have a menu that uses an array, like so:

$navarr = array("index.php" => "Home","cabinets.php"=>"Cabinet
Tops","cabinet_university.php"=>"Cabinet U","measurement.php" =>
"Measuring Tool");

and I loop through the array thusly (nav is a function I use to

<?php foreach($navarr as $key => $value)
{ ?>
<li><?php nav($key,$value,$thisurl,0)?></li>
<?php } ?>

What I would like to do is know that $thisurl is cabinets.php that
next page is countertops.php and the previous page is index.php, so I
could put something like <a href="<?php echo $nexturl; ?>">Next is <?
php echo
$nexttitle; ?></a> and I would be able to get this out of the array

I have tried current and next, but that is not working.  I know I am
close (at least I think I am), and I could use some direction here.


Adrienne Boswell at Home
Arbpen Web Site Design Services
Please respond to the group so others can share

Re: Finding the next element in an array

Quoted text here. Click to load it


I usually set up my menu array as an array of objects indexed by integers,
as in

$navarr[0]->url = 'index.php';
$navarr[0]->display = 'Home';
$navarr[0]->title = 'some title';

$navarr[1]->url = 'cabinets.php';
$navarr[1]->display = 'Cabinet choices';


They come out of my database like that, a number of SQL retrieved row
objects that I stuff into $navarr[].

Quoted text here. Click to load it

Then *I* loop through them thusly
for ($n = 0; $n < count($navarr); $n++)
    // do something with $navarr[$n], probably accessing $navarr[$n]->url
and other things
    if ($thisurl == $navarr[$n]->url)
        $navindex = $n;

Now I know where in the $navarr I am. I am at the $navindex'th item.

If ($navindex == 0) then I can pretty up the first item in the menu. If
($navindex == count($navarr) - 1) then I can pretty up the last item. And I
allways know what the previous and next items are.

Quoted text here. Click to load it

In my array the URL of the next page is $navarr[$navindex + 1]->url provided
that ($navindex < count($navarr) - 1);

Quoted text here. Click to load it

if ($navindex < count($navarr) - 1)
    echo something about $navarr[$navindex + 1];

if ($navindex > 0)
    echo something about $navarr[$navindex - 1];

Quoted text here. Click to load it

Coming from a long C++ background I tend to ignore current and next. I
usually like my arrays to be just that, arrays, and not ordered maps (which
PHP arrays actually are), unless of course I have need for something to be
an ordered map, as I often do.

Re: Finding the next element in an array

Quoted text here. Click to load it

Thanks for answering me.  I'm not sure if I was clear in my OP, or I'm
not sure if I understand (could be both - it's late and I'm tired).

I'm not interested in doing anything with the menu really - that's
what the nav function is for.  If $thispage is the page being passed
is the same as the one in the array, then it adds class=3D"thispage".

I would like to put this information at the bottom of the _current_
page, and perhaps in a link element in the head.

So, I know what $thispage is because it's at the top of the page.  I
need to be able to define $nextpage and $previouspage, without hard
coding them.

I'm going to bed now... I'll check in the morning.

Re: Finding the next element in an array [OT branch -- Usenet clients]

Quoted text here. Click to load it

For those of us that don't have access to real Usenet at work, I say
don't be ashamed!  Yeah, the user experience kinda sucks on GG.
Still, I'd rather have people participating than not contributing to
the group because they feel that their client doesn't live up to the
standards of the gnus users out there.

Re: Finding the next element in an array

Adrienne Boswell wrote:

Quoted text here. Click to load it

ACK.  Posting via GG is OK if you consider the caveats, i.e. borken
signature delimiters and automatic (but not displayed) line wraps.  Most
people don't, though, that's one major reason why GG is frowned upon on
Usenet.  (Another is spammers.)

Quoted text here. Click to load it

`foreach' is not advancing the internal array cursor/pointer, you would need
to use

  while ((list($key, $value) = each($navarr)))
    // ...

But current() cannot help you there, and prev() and next() move the internal
array cursor/pointer, which is not what you want (RTFM).

Quoted text here. Click to load it

Short answer: Use `for' instead.

Long answer:

If you are using `for' instead of `foreach' you have the (usually zero-
based) index of the array item; it is then trivial to access the next item.  
(You do _not_ have to modify your array structure for that.)  The downside
of this is that you have to (generally you should, for efficiency and
maintenance) assign $key and $value yourself.  This works if all your keys
are not numeric:

  $keys = array_keys($navarr);

  for ($key = 0, $len = $count($navarr); $i < $len; ++$i)
    $key = $keys[$key];
    $value = $navarr[$key];

    /* CAVEAT: Start looping at 1 or check $key before */
    $prev = $navarr[$key - 1];

    /* CAVEAT: Stop looping at $len - 1 or check $key before */
    $next = $navarr[$key + 1];

As an alternative, you can have the $navarr array item have an item
referring to the next item in the $navarr array.  You would compute that
reference value after you initialized the $navarr array:

  $navarr ...

  for ($i = 0, $len = count($navarr); $i < $len - 1; ++$i)
    // ...
    $navarr[$i] = array(
      'label' => $navarr[$i],

    $navarr[$i]['next'] =& $navarr[$i + 1];
Or you store the key of the next and previous item as string item of the
$navarr array item:

  $navarr = array(
   'index.php' => array(
     'label' => 'Home',
     'next'  => 'cabinets.php'

   'cabinets.php' => array(
     'label' => 'CabinetChoices',
     'prev'  => 'index.php'

But although this could be automated as well, it makes maintenance harder
and is increasing the risk of inconsistencies.

To work around that, you could instantiate a class that implements a linked
list.  This might be a bit overkill here, though, so I recommend to go with
arrays and use `for' instead.

But should you want to change the structure of $navarr in order to keep
`foreach', you do not need to count the index by yourself (which would make
maintenance a lot harder otherwise).  Either:

  $navarr[] = ...;
  $navarr[] = ...;

or (what I prefer in such a case):

  $navarr = array(
      'url' => '...',



  $navarr = array(
    new Foo(...),

Finally, do not use double quotes where no expansion is required for a
string value (see above).

Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Site Timeline