SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Finding the largest index num in an array?

    if i have something like

    PHP Code:
    while (some stuff here)
    {
      
    $value[0]="hello";
      
    $value[1]="world";
      if (
    something else to test)
        
    $value[2]="a";
      if (
    another thing to test)
        
    $value[3]="b";
      if (
    yet another test)
        
    $value[4]="c";

    for sure index 0 and 1 will be in the $value array if the while is true, but we dont know about 2, 3, or 4. Is there a way to find the largest index number in an array?

    btw, i dont mean count($value) but rather to check if $value[4] got in even if 2 or 3 didn't. this isn't the exact problem i have but the easiest way to explain it.

    right now my only guess would be to just do a hard-coded for loop with which goes up to 4 and comment that i should update the for loop should the array of $value increase to 5.
    Last edited by GeekSupport; Aug 9, 2002 at 09:50.

  2. #2
    PHP warrior dkode's Avatar
    Join Date
    Sep 2001
    Location
    Planet Namek
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    as far as enumerated arrays you simply do count($array_name) and that will give you the total number of elements in your array

    if you are talking about associative arrays you can get the last value by using:
    PHP Code:
    $last_entry array_pop($array_name); 
    array_pop will also work on enumerated arrays.
    http://www.php.net/array_pop/

    is that what you mean?
    "Mankind cannot define memory, yet it defines mankind"
    -- Project 2501, Ghost in the Shell

    Smarty | PEAR | PHP Manual | MySQL Manual

  3. #3
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dkode,

    i'm not sure if you read my first post before replying (i edited it several times to try to make it clearer). i dont need to find the totoal number of indexes in the array, but the "highest index number."

    for example, in the code above:

    lets say the while statement was true so $value[0] and $value[1] are stored into memory. Then the first two if statements were false but the last if statement was true. so now we have (in memory) $value[0], $value[1], $value[4]. What i need to find out is the highest index number which would be 4. if i did count($value), it would give 3 which is incorrect.

    i hope this makes it a little more clear
    Last edited by GeekSupport; Aug 9, 2002 at 09:56.

  4. #4
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can get the last key by using:

    echo end(array_keys($array));


  5. #5
    SitePoint Evangelist GeekSupport's Avatar
    Join Date
    May 2002
    Location
    Southern California
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jonsteele
    You can get the last key by using:

    echo end(array_keys($array));

    awesome! thanks a lot


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •