Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Array Help

  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Array Help

    Hi all,
    I need a little guidance about a project im working on. I pull two numbers from my database (say 3 and 7). What i need to do is find all the numbers in between 3 and 7 (inclusive) and store it in an array. Can someone help me go about doing this. THanks.
    Nick

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Try this...

    PHP Code:
    $num1 3;
    $num2 7;

    if (
    $num1 $num2) {
        for (
    $i $num2;$i <= $num1;$i++) {
            
    $numarray[] = $i;
        }
    } else {
        for (
    $i $num1;$i <= $num2;$i++) {
            
    $numarray[] = $i;
        }


  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This will put all the numbers into an array inclusive of the first and second number. It also doesn't matter which number is higher, the first or second.
    PHP Code:
    $num1 // your first db number
    $num2 // your second db number

    $start = ($num1 $num2 $num1 $num2);
    $end = ($num1 $num2 $num2 $num1);

    for(
    $i $start$i <= $end$i++) {
        
    $array[] = $i;
    }

    print_r($array); // view the numbers in the array 


  •  

    Posting Permissions

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