[thelist] PHP String Indexed Arrays

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


Matt,

I tried this:

----------------------------------
function print_sections() {
    global $sections;
    global $myhref;
    echo"<p class=\"subWORK\">";
    foreach ($sections as $sectionID => $section) {
        echo"<a href=\"$myhref?$sectionID\"> $section </a>";
        if ( !($section == end($sections)) )
        {
            echo"//";
        }
        else
        {
            //currently on last item
        }
    }


}

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

And it gave me this:


Case studies // by type by type by type by type by type by type by type by
type by type by type by type by type by type by type by type by type by type
by type by type by type by type by type....

Infinite loop.

I think it has to do with the fact that the end() funcion sets the array
pointer to the last element, which is great for the test, but messes with
the foreach() loop.

Ahhhh.....still stumped.

Thanks for the help anyway.


Jake






On 6/6/02 9:56 PM, "Warden, Matt" <mwarden at mattwarden.com> wrote:

> On Jan 1, Jacob Aust had something to say about [thelist] PHP String...
>
>> 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?
>
> try this inside your foreach loop:
>
> if ( !(array[item] == end(array)) )
> {
> print "/";
> }
> else
> {
> // currently on the last element
> }
>
>
> hth,
>
>
> --
> mattwarden
> mattwarden.com




More information about the thelist mailing list