By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,441 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,441 IT Pros & Developers. It's quick & easy.

last object of array

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Adam Carolla wrote:
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.

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


Brian
Jul 17 '05 #2

P: n/a
"Adam Carolla" <ac****@carollacarpentry.com> wrote in message
news:BzW8d.876$tU4.420@okepread06...
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.


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

- JP
Jul 17 '05 #3

P: n/a
"Adam Carolla" <ac****@carollacarpentry.com> wrote in message news:<BzW8d.876$tU4.420@okepread06>...
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.

$row = mysql_fetch_array($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
Jul 17 '05 #4

P: n/a
Brad Shinoda wrote:

for ( $i = 0; $i < count($row) - 1; $i++ )

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


Brian
Jul 17 '05 #5

P: n/a
"Default User" <fi********@boeing.com.invalid> wrote in message news:<I5********@news.boeing.com>...
Brad Shinoda wrote:

for ( $i = 0; $i < count($row) - 1; $i++ )

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


Bleh, typing &~ thinking :)
Jul 17 '05 #6

P: n/a
Brad Shinoda wrote:

"Adam Carolla" <ac****@carollacarpentry.com> wrote in message news:<BzW8d.876$tU4.420@okepread06>...
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.


$row = mysql_fetch_array($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


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?
Jul 17 '05 #7

P: n/a
Brad Shinoda wrote:
"Default User" <fi********@boeing.com.invalid> wrote in message
news:<I5********@news.boeing.com>...
Brad Shinoda wrote:

for ( $i = 0; $i < count($row) - 1; $i++ )

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


Bleh, typing &~ thinking :)


:)
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
Jul 17 '05 #8

P: n/a

Adam Carolla wrote:
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.

How about using the function reverse_array()?

<? while($row=mysql_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

Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.