WEB PHP: Array Help

Discussion in 'OT Technology' started by Guld, Aug 8, 2009.

  1. Guld

    Guld 昇龍拳

    Joined:
    Jun 18, 2004
    Messages:
    10,770
    Likes Received:
    0
    Location:
    Ontario, Canada
    I want to echo back certain bits of an array; however, the array is created using the str_split() function and based off an integer that will obtained from a database. Is there anyway to have echo back from a certain point on?

    Example:
    <php?
    $db_int = "12345"; //this an example of an integer obtained from the database
    $db_array = str_split($db_int, 1); //this will create an array with each digit as a position in the array
    echo $db_array[0]; // will echo back "1"
    echo $db_array[1]; // will echo back "2"
    echo $db_array[2]; // will echo back "3"
    echo $db_array[3]; // will echo back "4"
    echo $db_array[4]; // will echo back "5"
    ?>

    Now, given a situation like that, if the integer retrieved from the database was only "1234", then the code would mess up. The server would look to parse $db_array[4], but nothing would be there. Is there anyway to tell it to echo back from [3] on? Like echo $db_array[3+] so it would just go to 3 and just echo the rest of the array with it if it's there. How would you best handle echoing back arrays with variable positions without knowing how many positions they will have beforehand?

    Also, is there anyway to echo back 0 if the array position doesn't exist?

    Let me know if I'm not being clear enough. Thanks.
     
  2. pharmokan

    pharmokan OT Supporter

    Joined:
    Oct 18, 2002
    Messages:
    102,276
    Likes Received:
    499
    Location:
    L.A.
    for loop using length assigned to array $db_array ($db_array.length in this case = 5)

    for (i=0; i<=$db_array.length; i++) {

    echo $db_array;
    i++;
    }
     
  3. pharmokan

    pharmokan OT Supporter

    Joined:
    Oct 18, 2002
    Messages:
    102,276
    Likes Received:
    499
    Location:
    L.A.
    empty arrays already echo back NULL, so its practically 0 [ibHATE]. if you use that for loop that will never be the case where an empty value in an array will be output back to you.
     
  4. crazybenf

    crazybenf Active Member

    Joined:
    Nov 14, 2001
    Messages:
    15,578
    Likes Received:
    2


    if you're always wanting to start @ position 3, just change it so i=2.. it will start @ position 3 then cycle through the rest.
     
  5. Guld

    Guld 昇龍拳

    Joined:
    Jun 18, 2004
    Messages:
    10,770
    Likes Received:
    0
    Location:
    Ontario, Canada
    Ah, loops. Never even thought to use one to solve this issue. Got it working now. Thanks.
     
  6. pharmokan

    pharmokan OT Supporter

    Joined:
    Oct 18, 2002
    Messages:
    102,276
    Likes Received:
    499
    Location:
    L.A.
    YOU MAD OBLIVIOUS TO INTERMEDIATE PROGRAMMING CONCEPTS SON
     
  7. Guld

    Guld 昇龍拳

    Joined:
    Jun 18, 2004
    Messages:
    10,770
    Likes Received:
    0
    Location:
    Ontario, Canada
    I just started learning this shit like 5 days ago. :rofl:
     
  8. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Code:
    print_r($db_array);
    foreach($db_array as $this){
       if($this){
           echo $this . "\n";
       }else{
           echo "Empty." . "\n";
       }
    }
    
    Dumps the array as a whole so you can see what you've got then cycles through the array and if it exists print it out if not print out Empty.
     

Share This Page