Read the value of the last row in an array? PHP

sigmend

[H]ard|Gawd
Joined
Aug 6, 2003
Messages
1,303
Hey all,
I have an array with an unknown number of rows in it.
I want to read the value (into a variable) of the last row in the array. It will be a integer.
The data for the array comes from a MySQL query, if that is relevant at all.

Any idea how I can do this?

edit: I was able to butcher some code together to do this.
I would share it, but its so terrible I don't want anyone to see it
 

deuce868

Gawd
Joined
Feb 13, 2004
Messages
863
Well, I see two ways. First you can structure your query so that the last value is on top possibly. Use the ORDER BY part of the query to sort it so that it's up at the top.

The second way is to get a count of how many items are in the array and then just specify that value.

Code:
$my_array = array();

$count = count($my_aray);
$array_count = $count - 1 // since php arrays start aty 0 you need to subtract one

$last_value = $myarray["$array_count"];
 

tim_m

i'm so nice
Joined
Feb 10, 2003
Messages
5,540
^^ this assumes you are using mysql_fetch_row, not mysql_fetch_assoc, just to clarify
 
Top