Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-20-2009, 10:17 PM   PM User | #1
trazix
New Coder

 
Join Date: Jul 2009
Posts: 45
Thanks: 11
Thanked 0 Times in 0 Posts
trazix is an unknown quantity at this point
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")
trazix is offline   Reply With Quote
Old 09-20-2009, 10:32 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Old 09-20-2009, 10:42 PM   PM User | #3
trazix
New Coder

 
Join Date: Jul 2009
Posts: 45
Thanks: 11
Thanked 0 Times in 0 Posts
trazix is an unknown quantity at this point
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.
trazix is offline   Reply With Quote
Old 09-21-2009, 01:02 AM   PM User | #4
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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
)
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 09-27-2009, 07:49 PM   PM User | #5
trazix
New Coder

 
Join Date: Jul 2009
Posts: 45
Thanks: 11
Thanked 0 Times in 0 Posts
trazix is an unknown quantity at this point
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..
trazix is offline   Reply With Quote
Old 09-27-2009, 08:13 PM   PM User | #6
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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.
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 09-27-2009, 08:20 PM   PM User | #7
trazix
New Coder

 
Join Date: Jul 2009
Posts: 45
Thanks: 11
Thanked 0 Times in 0 Posts
trazix is an unknown quantity at this point
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.
trazix is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:53 AM.


Advertisement
Log in to turn off these ads.