last object of array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Carolla

    last object of array

    Hello,

    I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I need to write "last item".

    Is there a way to do this: figure out how many array values there are then out put all but the last one, then write some text, then write the last value of the array.

    Thanks in advance.
  • Default User

    #2
    Re: last object of array

    Adam Carolla wrote:
    [color=blue]
    > Hello,
    >
    > I have a script that is echo'ing the values of a mysql query using a
    > while. What I need to do is right before the last value is echo'd I
    > need to write "last item".
    >
    > Is there a way to do this: figure out how many array values there are
    > then out put all but the last one, then write some text, then write
    > the last value of the array.[/color]


    Either count() or sizeof() returns the number of elements in an array.




    Brian


    Comment

    • kingofkolt

      #3
      Re: last object of array

      "Adam Carolla" <aceman@carolla carpentry.com> wrote in message
      news:BzW8d.876$ tU4.420@okeprea d06...[color=blue]
      > Hello,
      >
      > I have a script that is echo'ing the values of a mysql query using a[/color]
      while. What I need to do is right before the last value is echo'd I need to
      write "last item".[color=blue]
      >
      > Is there a way to do this: figure out how many array values there are then[/color]
      out put all but the last one, then write some text, then write the last
      value of the array.[color=blue]
      >
      > Thanks in advance.[/color]

      $count=count($a rray);
      $i=1;
      foreach ($array as $item) {
      if ($i==$count) {
      print "Last item: ";
      }
      print $item . "<br />\n";
      $i++;
      }

      - JP


      Comment

      • Brad Shinoda

        #4
        Re: last object of array

        "Adam Carolla" <aceman@carolla carpentry.com> wrote in message news:<BzW8d.876 $tU4.420@okepre ad06>...[color=blue]
        > Hello,
        >
        > I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I need to write "last item".
        >
        > Is there a way to do this: figure out how many array values there are then out put all but the last one, then write some text, then write the last value of the array.
        >
        > Thanks in advance.[/color]


        $row = mysql_fetch_arr ay($result);

        for ( $i = 0; $i < count($row) - 1; $i++ )
        {
        echo $row[$i];
        }
        echo "last value";
        echo $row[$i]; // not sure if $i or $i + 1, you'll have to check

        Comment

        • Default User

          #5
          Re: last object of array

          Brad Shinoda wrote:

          [color=blue]
          > for ( $i = 0; $i < count($row) - 1; $i++ )[/color]


          You don't really want to call count() every single loop cycle, do you?




          Brian

          Comment

          • Brad Shinoda

            #6
            Re: last object of array

            "Default User" <first.last@boe ing.com.invalid > wrote in message news:<I583z6.Ar F@news.boeing.c om>...[color=blue]
            > Brad Shinoda wrote:
            >
            >[color=green]
            > > for ( $i = 0; $i < count($row) - 1; $i++ )[/color]
            >
            >
            > You don't really want to call count() every single loop cycle, do you?
            >[/color]

            Bleh, typing &~ thinking :)

            Comment

            • Anonymous

              #7
              Re: last object of array

              Brad Shinoda wrote:[color=blue]
              >
              > "Adam Carolla" <aceman@carolla carpentry.com> wrote in message news:<BzW8d.876 $tU4.420@okepre ad06>...[color=green]
              > > Hello,
              > >
              > > I have a script that is echo'ing the values of a mysql query using a while. What I need to do is right before the last value is echo'd I need to write "last item".
              > >
              > > Is there a way to do this: figure out how many array values there are then out put all but the last one, then write some text, then write the last value of the array.
              > >
              > > Thanks in advance.[/color]
              >
              > $row = mysql_fetch_arr ay($result);
              >
              > for ( $i = 0; $i < count($row) - 1; $i++ )
              > {
              > echo $row[$i];
              > }
              > echo "last value";
              > echo $row[$i]; // not sure if $i or $i + 1, you'll have to check[/color]

              As far as I know count returns the number of items in the array, which
              are numbered 0 to count-1.

              With these lines you print all items first, then 'last value' and then a
              value that doesn't exist. You meant to write count($row) -2, right?

              Comment

              • Default User

                #8
                Re: last object of array

                Brad Shinoda wrote:
                [color=blue]
                > "Default User" <first.last@boe ing.com.invalid > wrote in message
                > news:<I583z6.Ar F@news.boeing.c om>...[color=green]
                > > Brad Shinoda wrote:
                > >
                > >[color=darkred]
                > > > for ( $i = 0; $i < count($row) - 1; $i++ )[/color]
                > >
                > >
                > > You don't really want to call count() every single loop cycle, do
                > > you?
                > >[/color]
                >
                > Bleh, typing &~ thinking :)[/color]

                :)


                I'm not sure how expensive the count() function is in PHP. Presumably
                an array is some sort of object (although basically opaque) and
                probably contains the current size in a member variable or somesuch,
                like a C++ vector. Then count() would just return that value.

                For a small array this probably makes little difference, but for a
                large one every little bit helps, especially with a not particularly
                speedy interpreted language.

                For a compiled language, the compiler would probably examine the loop,
                note that the array never changed, then optimize away the function call.



                Brian

                Comment

                • Ken Robinson

                  #9
                  Re: last object of array


                  Adam Carolla wrote:[color=blue]
                  > Hello,
                  >
                  > I have a script that is echo'ing the values of a mysql query using a[/color]
                  while. What I need to do is right before the last value is echo'd I
                  need to write "last item".[color=blue]
                  >
                  > Is there a way to do this: figure out how many array values there are[/color]
                  then out put all but the last one, then write some text, then write the
                  last value of the array.

                  How about using the function reverse_array() ?

                  <? while($row=mysq l_fetch_array($ result)) {
                  $rev_row = reverse_array($ row);
                  $next_to_last = $rev_row[1];
                  for ($i=0;$i<count( $row);$i++) {
                  echo $row[$i]."<br>\n";
                  if ($row[$i] == $next_to_last) echo "Last Item<br>\n"; }
                  }
                  ?>

                  The above hasn't been tested... YMMV ...

                  Ken

                  Comment

                  Working...