[thelist] PHP String Indexed Arrays

Jacob Aust mail_lists at jakesdiner.com
Fri Jun 7 00:50:01 CDT 2002


Hi...

I'm somewhat of a beginner in PHP and I have been beating my head against
this one today.

I have a string indexed array that I am stepping through and formatting with
HTML. I am trying to figure out a way to find out when I am at the last item
in the array. My loop writes the value of each element and then inserts a
slash for separation from the next element. However, after the last item I
want to not output the slash.

Is it possible to referenct items in a string indexed array by a numeric
index? I can easily get the number of items in the array with count(), but
how do I tell if the current item in a foreach() loop is the last one?

Here's my code:
-----------------------

$myhref = "index.php";

$sections = array ("case" => "case studies", "client" => "by client", "type"
=> "by type");


function print_sections() {
    global $sections;
    global $myhref;
    echo"<p class=\"subWORK\">";
    foreach ($sections as $sectionID => $section) {
        echo"<a href=\"$myhref?$sectionID\"> $section </a>";
        $LastElement = ????????
        if (!$LastElement) {
                echo"//";
        }
    }

}

-------------------------

What I am trying to do is figure out how to set $LastElement to FALSE when
the item is anything but the last element of the array and TRUE when it is
the last element.


Any suggestions?



Jake Aust




More information about the thelist mailing list