View Full Version : Using ROUND() on Array Elements

sir pannels
02-05-2007, 10:47 PM
Hi All,

Just a quick one I hope. I have an array with 10 elements and each element as a numerical value. I want to use ROUND to round each value to 0 decimal places and have tried the following:

$element[1] = round($array[1]);

However that did not work, so I tried rounding the entire array at once using:

$newarray[] = round($array[]);

No work either. No errors, they just set the newarray to 0, no matter what the values before.

Any ideas?
Many thanks for your times.

Sir P :D

02-05-2007, 11:00 PM
How about going through them in a foreach:

$element = array(1,2,1.5,2.2,5.9); //Test Array
print_r($element); //Show Before
foreach(array_keys($element) AS $ek){ //Get key of each value so it can be set back to itself
$element[$ek] = round($element[$ek]); //Do rounding
print_r($element); //Show array afterwards

Just thinking about it your first example should have worked really so not sure what went wrong there... my snippet at least works for me :P

02-05-2007, 11:04 PM
Are you sure your array elements are considered numbers?

i.e., I don;t know for sure if these 2 declarations are interpreted the same way:

$array[1] = 1.5;
$array[1] = '1.5';

02-05-2007, 11:12 PM
Chump2877: just tested your idea as its a fair assumption but round() still actually works even if the numbers are set with quotes or even typecast as a string, appears to be quite forgiving!

sir pannels
02-05-2007, 11:16 PM
Hi anarchy3200 , thanks for your snipplet, yeh yours is pretty much same as mine and has the same effect, everything gets changed to zero. D'oh. Cheers for you time though.

Hey Chump, I put your code in to debug and damn, every value was indeed a number.. which brings me no closer to solving. Dang.

Thanks again for you time both of you.

ralph l mayo
02-05-2007, 11:20 PM
The usual idiom for applying and function to an array and substituting its values with the result is map, although it's kind of clunky in PHP:

$ints = array_map('round', $floats);

02-05-2007, 11:22 PM
Ok, so after you try ralph l mayo's idea, whats the outcome if you try using a different function like ceil() or floor(), same 0 output?

sir pannels
02-05-2007, 11:59 PM
Ok anarchy, let me just try ralphs then...

Ralph, tried it but maybe im misunderstanding the syntax, i have not used array_map before, would I be using ..

$array = array_map('round', $array);

? I get errors that the second resource should be an array. Is that reffering to the word 'round', is that the bit I misunderstood?

Thanks for all your time everyone :)

02-06-2007, 01:05 AM
Can you do:


And post the output here in this thread...

Something doesn;t add up here...I don;t see why you are having this problem...

Once I see the output, I can create my own array with the same key-value pairs, and recreate your situation...And see if I get the same results

sir pannels
02-06-2007, 11:31 AM
Ok Chump great thanks...

If I do not do any rounding and var dump the array I get the following.

array(10) { [0]=> float(18.8583358187) [1]=> float(13.8956158664) [2]=> float(752.348344766) [3]=> int(0) [4]=> int(0) [5]=> float(129.030718759) [6]=> int(0) [7]=> int(0) [8]=> int(0) [9]=> int(0) }

If I try and round using

$array[$i] = round($array[$i]);

And then var dump I get the following.

array(10) { [0]=> float(0) [1]=> float(0) [2]=> float(0) [3]=> float(0) [4]=> float(0) [5]=> float(0) [6]=> float(0) [7]=> float(0) [8]=> float(0) [9]=> float(0) }

Thanks for all your time

sir pannels
02-06-2007, 12:01 PM
I have resolved it!!

Not actually quite sure how, though I believe it was a typo somewhere. I just wrote it again using the $array[$i] = round($array[$i]); approach in a whileloop and it has worked great, correct results now.

I looked back at the other code and still cannot see a typo, but there must be.

When I staretd this and posted yesterday it was about midnight after a long day hha.

I would like to thank you all for your kind help and support.

All the best,
Sir P

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum