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 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Populating an array problem

    I need help to create/populate an array with numbers and I cant find a solution,

    599 divided by 100 = 5.99

    I would like the array to contain the ellements bellow

    array("100", "100", "100" "100", "100", "99")

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I'm not quite sure I follow you; the first example uses ints and doubles, while the second is for an array.
    For the array:
    PHP Code:
    $aNums = array(10010010010010099); 
    For the calculation from the array:
    PHP Code:
    $sum array_sum($aNums);
    printf('%d divided by 100 = %0.2f'$sum, ($sum 100)); 
    That what you're looking for?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts
    599 could be any number (it comes from the width of an image file) but what ever the number is it will be divided by 100 so the array should be like for 345 be (100, 100, 100, 45)

    Basicaly the array numbers should be 100 until there is a remainder left 45.

    I need the array to create itself based on the value of an images width.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Will this do?
    PHP Code:
    function whatever$width$num )
    {
        
    $output array_fill0floor$width $num ), $num );
        if ( 
    $mod $width $num )
        {
            
    $output[] = $mod;
        }
        return 
    $output;
    }

    print_rwhatever345100 ) ); 
    Output:
    Code:
    Array
    (
        [0] => 100
        [1] => 100
        [2] => 100
        [3] => 45
    )

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts
    kbluhm your code works but as I am not competent enough at this php programming I cant get yours to work as I want it, I dont want to write the details to screen just to have an array created dynamicaly that will have as it's elements the number "$num" as many times as it will divide into the "$width" value and then the last element of the array will be the remainder left in the sum.

    The
    PHP Code:
    $outputx array_fill0floor$width $num ), $num ); 
    will create the array except the last element the remainder. Can this line be modified to do this. In your function you had the values entered at the end
    PHP Code:
    print_rwhatever345100 ) ); 
    I dont want to have to do ths as each image size could be different.

    Here is the code that i have so far for testing the arrays

    PHP Code:

    <?php

    $my_image 
    array_values(getimagesize('test.jpg'));

    $width $my_image[0];
    $height $my_image[1];

    $num 100;

    $outputx array_fill0floor$width $num ), $num );

    $outputy array_fill0floor$height $num ), $num );

    echo 
    '<b>WIDTH:</b>'.$width;
    echo 
    '<b>HEIGHT:</b>'.$height;

    //view the array
    print_r($outputx);
    print_r($outputy);

    ?>
    And a link
    Last edited by trazix; 09-27-2009 at 07:51 PM.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I'm not following. You asked to have a width and divisor passed. If you pass those to whatever() -- which could be called anything, I just chose that name for the example-- it returns an array in the format you requested. How you obtain the width is entirely up to you.

  • #7
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts
    kbluhm Your code is 100% top class and outputs exactly what i asked for but I am a total novice at programing and would like to integrate your code with mine but I've trying and getting nowhere with it.


  •  

    Posting Permissions

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