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

1. 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")

• 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?

• 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.

• 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
)

• 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);

?>

• 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.

• 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
•