View Full Version : Populating an array problem
09-20-2009, 11:17 PM
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")
09-20-2009, 11:32 PM
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:
$aNums = array(100, 100, 100, 100, 100, 99);
For the calculation from the array:
$sum = array_sum($aNums);
printf('%d divided by 100 = %0.2f', $sum, ($sum / 100));
That what you're looking for?
09-20-2009, 11:42 PM
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.
09-21-2009, 02:02 AM
Will this do?
function whatever( $width, $num )
$output = array_fill( 0, floor( $width / $num ), $num );
if ( $mod = $width % $num )
$output = $mod;
print_r( whatever( 345, 100 ) );
 => 100
 => 100
 => 100
 => 45
09-27-2009, 08:49 PM
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.
$outputx = array_fill( 0, floor( $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
print_r( whatever( 345, 100 ) ); 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
$my_image = array_values(getimagesize('test.jpg'));
$width = $my_image;
$height = $my_image;
$num = 100;
$outputx = array_fill( 0, floor( $width / $num ), $num );
$outputy = array_fill( 0, floor( $height / $num ), $num );
//view the array
And a link (http://yabadabadoo.blackapplehost.com/IMAGE%20SPLIT/Test2xxxxxxxxxxx.php)
09-27-2009, 09:13 PM
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.
09-27-2009, 09:20 PM
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.